0% found this document useful (0 votes)
15 views10 pages

CSC 3155

The document consists of multiple C programming code snippets that demonstrate various functions such as calculating factorials, summing numbers, finding Fibonacci series, and performing matrix operations. Each section includes user input prompts and basic logic for processing data, along with comments explaining the code. The snippets illustrate fundamental programming concepts and techniques in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

CSC 3155

The document consists of multiple C programming code snippets that demonstrate various functions such as calculating factorials, summing numbers, finding Fibonacci series, and performing matrix operations. Each section includes user input prompts and basic logic for processing data, along with comments explaining the code. The snippets illustrate fundamental programming concepts and techniques in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NUMBER1.

#i
ncl
ude<st
dio.
h>

l
ongi
ntf
act
ori
al(
intn)
;

i
ntmai
n()

I
ntn;

pr
int
f(“
Ent
eraposi
ti
vei
nteger
:“)
;

scanf
(“%d”
,&n)
;

pr
int
f(“
Fact
ori
alof%d=%l
d”,
n,f
act
ori
al(
n))
;

r
etur
n0;

l
ongi
ntf
act
ori
al(
intn){

i
f(n>1)

r
etur
nn*f
act
ori
al(
n-1)
;

el
se

r
etur
n1;

(
ii
)

#i
ncl
ude<st
dio.
h>

i
ntsum (
intn)

I
ntn;

I
ntadd=0;

pr
int
f(“
Ent
erVal
ueofn”
)

i
f(n>10)

pr
int
f(“
numbershoul
dbel
esst
han10”
);
}

el
se{

scanf(
“%d”
,&n)
;

f
or(
inti
=1;
i<=n;
i++)

add+=I
;

r
etur
nadd;

(
b)

Commenti sapr ogr


ammer -
readabl
eexplanati
onorannotati
oninthesourcecodeofa
computerprogram.Theyareaddedwiththepur poseofmakingthecprogram
sour
cecodeeasi erforhumanstounderstand,andaregener
all
yignor
edbycompi l
ers
andint
erpr
eters.
(
ii
)
I
f(t
estexpr
essi
on)
{
/
/code
}
(
c)
#i
ncl
ude<st
dio.
h>
i
ntmai
n()
{
i
nta,
b;
i
ntdi
ff
;

pr
int
f(“
Ent
erf
ir
stnumber
:“)
;
scanf
(“%d”
,&a)
;
pr
int
f(“
Ent
ersecondnumber
:“)
;
scanf
(“%d”
,&b)
;
/
/checkcondi
ti
ont
oident
if
ywhi
chi
slar
gestnumber
I
f(
a>b)
di
ff=a-
b;
el
se
di
ff=b-
a;
pr
int
f(“
dif
fer
encebet
ween%dand%di
s=%d”
,a,
b,
dif
f);
r
etur
n0; }

NUMBER2.

#i
ncl
ude<st
dio.
h>
i
ntmai
n(v
oid){

i
ntnumber
s[10]
;
i
nti
,i
ndex=6;
f
or(
i=0;
i<10;
i++)
{

scanf(“
%d’
,number
s[i
])
;
numbers[
i]=i
;
print
f("
%i"
,number
s[i
])
;
}
i
ntnum =numbers[i
]=i;

f
or( i
=0; i
<num; i
==)
{
sum+=number s[i
]
}
fl
oatavg=sum/ i
ndex;
pri
ntf(
‘Theaveragesalesofeachsal
esagenti
s:%f
’,av
g);
f
or( i
=0; i
<i
ndex;i
==)
{
sum+=arr[
i]
}
fl
oatavg=sum/si
ze;
pri
ntf(
‘Theaver
agesal
esf
oreachdayi
s:%f
’,av
g);

}
min=max=number [10]
;
for(
i=1; i
<10;i
++)
{
if
(mi n>number[
i]
)
mi n=number[i
];
if
(max<number [i
])
max=number [
i]
;
}
pri
ntf("
mi ni
mum ofeachdayi s:%d",
min);
pr
intf(
"\nmaximum ofeachdayi s:%d",
max);
for(i
=0;i<10;i++){
i
f(number [
i]<MAX){
pri
ntf(“A[
%d]=%. 1f
”,I
,number[
i]
);
}
}

NUMBER3.

#i
ncl
ude<st
dio.
h>
i
ntmai
n(){
i
nti
,n,
t1=0,
t2=1,
next
Ter
m;
pr
int
f(“
Ent
ert
henumberoft
erms:
“);
scanf
(“%d”
,&n)
;
pr
int
f(“
Fibonacci
Ser
ies:
”)

f
or(
i=1,
i<=n;
++i
){
pr
int
f(“
%d,
“,t
1);
next
Ter
m =t
1+t
2;
t
1=t
2;
t
2=next
Ter
m;}
}
(
b)
#i
ncl
ude<st
dio.
h>
I
ntmai
n(){
i
ntn=100000000;
i
nti
;
f
or(
i=1;
i<=n;
i++)
{
i
f(i
/2==0)
{
pr
int
f(“
%d”
,i)
;
}
else{
r
etur
n0;
}
}
}
(
bii
)
1.i
nti
nteger
s--
-- 42,
60634,
-8,
0

2.doubl
e--
--0.
039,
-10.
2,4.
2E+72

3.char'
a'
,'
B'
,'
&'
,'
6'

4.Bool
eanl
ogi
cal
val
uest
rue,
fal
se

NUMBER4.

#i
ncl
ude<st
dio.
h>

i
ntmai
n()

chars1[
100]
,s2[
100]
;

i
nti
,j
,fl
ag=1;

pr
int
f("
Ent
erast
ri
ng:
");

scanf
("%[
^\n]
",
ch)
;
/
/fi
ndSt
ri
ngl
engt
h

f
or(
i=0;
s1[
i]
!='
\0'
;
i++)
;

/
/Copy
ingst
ri
ngi
nrev
erseor
der

f
or(
i-
-,
j=0;
i>=0;
i-
-,j
++)

s2[
j]
=s1[
i]
;

s2[
j]
='
\0'
;

/
/St
ri
ngcompar
isi
on

f
or(
i=0;
s1[
i]
!='
\0'
;
i++)

i
f(
s1[
i]
!=s2[
i]
)

f
lag=0;

br
eak;

i
f(
fl
ag==1)

pr
int
f("
Str
ingi
sPal
i
ndr
ome.
")
;

el
sepr
int
f("
Str
ingi
snotPal
i
ndr
ome.
")
;

r
etur
n0;

(
ii
)

ANWSER.
#i
ncl
ude<st
dio.
h>
v
oidmai
n(){

i
ntnum,
m,n;

cl
rscr
();

pr
int
f("
Ent
err
ange(
m,n):
");

scanf(
"%d%d"
,&m,
&n)
;

f
or(
num =m;
num <=n;
num++){

pr
int
f("
%d"
,num)
;

get
ch(
);

NUMBER5.

#i
ncl
ude<st
dio.
h>
i
ntmai
n(){
i
ntmat
ri
x_r
ow=5;
i
ntmat
ri
x_col
umn=5;
i
ntmat
ri
x_a[
5][
5];
i
ntmat
ri
x_b[
5][
5];
i
ntmat
ri
x_sum[
5][
5];
i
ntsum;

pr
int
f(“
Ent
erel
ement
sformat
ri
xA:
‘);
f
or(i
nti
=0;
i<mat
ri
x_r
ow;
++i
)
f
or(
intj
=0;
j<mat
ri
x_col
umn;
++j
){
pr
int
f(“
Ent
erel
ement
sa%d%d:
“,i
+1,
j+1)
;
scanf
(“%d”
,&mat
ri
x_a[
i]
[j
])
;
}
pr
int
f(“
Ent
erel
ement
sformat
ri
xB:
‘);
f
or(i
nti
=0;
i<mat
ri
x_r
ow;
++i
)
f
or(
intj
=0;
j<mat
ri
x_col
umn;
++j
){
pr
int
f(“
Ent
erel
ement
sa%d%d:
“,i
+1,
j+1)
;
scanf
(“%d”
,&mat
ri
x_b[
i]
[j
])
;
}
f
or(i
nti
=0;
i<mat
ri
x_r
ow;
++i
)
f
or(
intj
=0;
j<mat
ri
x_col
umn;
++j
){
mat
ri
x_sum[
i]
[j
]=mat
ri
x_a[
i]
[j
]+mat
ri
x_b[
i]
[j
];
}
pr
int
f(“
Sum oft
womat
ri
ces:
‘);
f
or(i
nti
=0;
i<mat
ri
x_r
ow;
++i
)
f
or(
intj
=0;
j<mat
ri
x_col
umn;
++j
){
pr
int
f(“
%d“
,mat
ri
x_sum[
i]
[j
])
;
i
f(j
==mat
ri
x_col
umn–1)
{
pr
int
f(“
”);
}
i
nti
,j;
f
or(
i=0;
i<mat
ri
x_r
ow;
i
++)
{
f
or(
j=0;
j<mat
ri
x_r
ow;
j
++)
{
i
f(
i==j
)
{
sum=sum+mat
ri
x_sum[
i]
[j
];
pr
int
f(“
Thesum ofdi
agonal
element
sofasquar
emat
ri
x=%d”
,sum)
;
}
}
}
}
}
(
b)

#i
ncl
ude<st
dio.
h>
i
ntbi
nar
y_conv
ersi
on(
int
);

i
ntmai
n()

i
ntnum,
bin;

pr
int
f("
Ent
eradeci
mal
number
:")
;

scanf
("%d"
,&num)
;

bi
n=bi
nar
y_conv
ersi
on(
num)
;

pr
int
f("
Thebi
nar
yequi
val
entof%di
s%d\
n",
num,
bin)
;

i
ntbi
nar
y_conv
ersi
on(
intnum)

i
f(num ==0)

r
etur
n0;

el
se

r
etur
n(num %2)+10*bi
nar
y_conv
ersi
on(
num /2)
;

You might also like