Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

S.

Topi cs Pr
ograms
No.
1 Pr ogr ammi ng Calculatingt hefactor
ial
ofanumber.
basics
Datat ypes Fi
rstMet hod:
Oper ator s #i
ncl ude<st dio.
h.>
and i
ntmai n()
expressi ons {
I/
O i
nti ,n,
f;
Statement s printf("
enteranumber ..
.!
\n"
);
Condi ti
onal scanf ("%d",&n)
;
statement s i
f(n==0)
Loopi ng f=1;
statement s else
Uncondi ti
onal {
branchi ng for(i
=1;i<=n;
i++)
{
f=f *i;
}
}
print f
("Factori
alval
ueof%di s=%d",
n,
f)
;
}

Output:
enteranumber.
..
!
5
Factor
ialv
alueof5i
s=120

SecondMet
hod:Fact
ori
alusi
ngFunct
ions

#i
nclude<st di
o.h>
i
ntmai n()
{
i
ntn,fact1;
i
ntfactorial
(i
nt)
;
pri
ntf(
"Enteranumber
:")
;
scanf("
%d" ,
&n);

pr
int
f("
Thef
act
ori
alv
laueof%di
s=%d"
,n,
fact
ori
al(
n))
;
}

i
ntfactor
ial
(i
ntx)
{
intfact=1;
if(
x==0)
ret
urnfact;
else
fact=x*fact
ori
al(
x-1)
;
retur
n(f
act
);
}

Output:
Enteranumber :
4
Thef act
ori
alval
ueof4is=24
2 Greatestoft
hreeint
eger
susi
ngt
ernar
yoper
ator

Fi
rstMet
hod:usi
ngTer
nar
yOper
ator

#i
ncl
ude<stdi
o.h>
i
ntmain()
{
i
ntnum1, num2,num3, l
argest
;
pri
ntf
("Enterthefi
rstnumber\n"
);
scanf(
"%d" ,
&num1) ;
pri
ntf
("Enterthesecondnumber \
n")
;
scanf(
"%d" ,
&num2) ;
pri
ntf
("Enterthethi
rdnumber\n")
;
scanf(
"%d" ,
&num3) ;

lar
gest=(num1>num2)&&( num2>num3)?num1: (
(num2>num3)?
num2:
num3);
pri
ntf
("Thel
argestoft
hreenumbersi
s=%d",
l
argest
);
}

Output
:
Ent
erthefi
rstnumber
45
Ent
erthesecondnumber
67
Ent
erthethi
rdnumber
78
Thelar
gestofthr
eenumber
sis=78

SecondMet
hodusi
ngi
fel
se

#i
ncl
ude<stdi
o.h>
i
ntmain()
{
i
ntx,y,
z;
pri
ntf
("Ent
erthefi
rstnumber
\n"
);
scanf(
"%d",
&x);
pri
ntf
("Ent
erthesecondnumber
\n"
);
scanf
("%d",
&y);
pri
ntf
("Ent
erthethi
rdnumber
\n"
);
scanf
("%d",
&z);

i
f(
x>y&&x>z)
pri
ntf("
%di sgreat
erthan%dand%d"
,x,
y,
z);
el
sei f
(y>x&&y >z)
printf
("%disgreat
erthan%dand%d",
y,
x,
z);
else
printf
("%disgreat
erthan%dand%d",
z,
x,
y);
return0;
}

Out
put

Ent
erthefi
rstnumber
45
Ent
erthesecondnumber
67
Ent
erthethi
rdnumber
78
Thelar
gestofthr
eenumber
sis=78

78i
sgr
eat
ert
han45and67

3 Checkwhet
hert
hegi
vennumberi
spal
i
ndr
omeornot
.

#i
ncl
ude<stdio.h>
i
ntmain()
{
i
ntn, ori
ginal
,r
emainder
,r
eversed=0;
pri
ntf("
Enterthenumber\n"
);
scanf("
%d" ,
&n);
ori
ginal=n;

whil
e(n! =0)
{
remainder=n%10;
rever
sed=r ever
sed*10+r emainder
;
n=n/ 10;
}
i
f(reversed==or i
ginal
)
pr
intf
("Thegivennumer ici
spal
indrome")
;
el
se
pr
intf
("Thegivennumer ici
snotpali
ndr
ome");
}

Out
put

Entert
henumber
16461
Thegivennumberi
spal
i
ndr
ome

Ent
erthenumber
213
Thegi
v ennumberisnotapal
indr
ome
4 Gener
at eFi
bonacci
numbers.

#i
ncl
ude<st
dio.h>
i
ntmain(
)
{
i
ntx,
y,
z,n,i
;
x=0;
y=1;

pri
ntf
("Ent
ertheval
ueofn\
n")
;
scanf
("%d",
&n);

pr
int
f("
%d\
t",
x);
pr
int
f("
%d\
t",
y);

f
or(
i=0;
i<n-
2;i
++)
{
z=x +y;
pri
ntf(
"%d\
t",
z);
x=y ;
y=z;
}
}

Out
put
Ent
ert
hev
alueofn
5
0 1 1 2 3

Ent
ert
hev
alueofn
9
0 1 1 2 3 5 8 13 21
5 Pr
intnumbersfr
om 1t
o100wi
thoutusi
ngl
oops.
#i
nclude<st
dio.
h>
i
ntmai n(
)
{
i
nti=0;

begin:
i=i+1;
pri
ntf
("%d\
n",
i);

i
f(i<100)
{
got
obegin;
}
r
eturn0;
}
Out
put
1
2
3
.
.
.
100

6 Checki
fagi
veny
eari
sal
eapy
earornot
.

Fi
rstMet hod
#i
nclude<stdi
o.h>
i
ntmai n(
)
{
i
nty ear;
pri
ntf("
Entertheyear
\n"
);
scanf("
%d" ,
&year
);

i
f(
((y
ear%4==0)&&( y
ear
%100!=0) )|
|(y
ear
%400==0)
)
pri
ntf
("%di
saleapyear
",
year)
;
el
se
pri
ntf
("%di
snotaleapyear
",year
);

ret
urn0;
}
Out
put

Entert
hey
ear
2004
2004isal
eapyear

Entert
heyear
2015
2015isnotal
eapy
ear

SecondMet
hod

#i
ncl ude<stdio.
h>
#i
ncl ude<ctype.h>
i
ntmai n()
{
//unsignedintyear;
i
nty ear;
printf(
"Ent
ertheYear:"
);
scanf (
"%d",
&year);

//leapy earifperfect
lydivisi
bleby400
i
f( year%400==0)
{
print
f ("
%di saleapy ear.
",y
ear)
;
}
//notal eapy earifdivi
sibleby100
//butnotdi v i
sibl
eby400
elseif(year%100==0)
{
print
f ("
%di snotal eapyear.
\n"
,year
);
}
//leapy earifnotdivi
sibleby100
//butdi vi
sibleby4
elseif(year%4==0)
{
print
f ("
%di saleapy ear.
",y
ear)
;
}
//allothery earsarenotleapy ears
else
{
print
f ("
%di snotal eapyear.
\n"
,year
);
}
r
etur
n0;
}
Out
put

Entert
hey
ear:
2004
2004isal
eapyear

Entert
heyear
:2015
2015isnotal
eapyear
CPr
ogr
am Tut
ori
al

1.Calcul
ati
ngthef act
ori
alofanumber
.(Edi
tthegi
venpr
ogr
am t
orunt
hist
est
cases)
TestCases:1

Sampl
eInput
:Ent
ert
hev
alue=0

Sampl
eOut
put
:Fact
ori
alof0i
s1

TestCases:
2

Sampl
eInput
:Ent
ert
hev
alue=-
5

Sampl
eOut
put
:Ent
ert
heposi
ti
veVal
ue

TestCases:
3

Sampl
eInput
:Ent
ert
hev
alue=0.
5

Sampl
eOut
put
:Ent
ert
hei
ntegerv
alue

2.Greatestoft
hreei
nteger
susi
ngt
ernar
yoper
ator(
Edi
tthepr
ogr
am t
orunt
het
est
cases)

TestCases:
1

Sampl
eInput
:Ent he1stnumber=0
ert

Ent he2ndnumber=0
ert

Ent he3rdnumber=0
ert

Sampl
eOut
put
:Ent
erdi
ff
erentnumber
s

TestCases:
2

Sampl
eInput
:Ent he1stnumber=10
ert

Ent he2ndnumber=-
ert 10

Ent he3rdnumber=100
ert

Sampl
eOut
put
:Lar
gestNumberi
s100

TestCases:
3
Sampl
eInput
:Ent he1stnumber=10.
ert 5

Ent he2ndnumber=-
ert 10.
5

Ent he3rdnumber=2.
ert 10

Sampl
eOut
put
:Lar
gestNumberi
s10.
5

TestCondi
ti
on:
4

Smal
l
estof4number
susi
ngt
ernar
yoper
ator

3.Checkwhethert
hegivennumberi
spal
i
ndr
omeornot
.(Edi
tthepr
ogr
am t
orun
thet
estcases)
TestCases:1

Sampl
eInput
:Ent
ert
henumber=0

Sampl
eOut
put
:Thenumber0i
spal
i
ndr
ome

TestCases:
2

Sampl
eInput
:Ent
ert
hev
alue=-
12321

Sampl
eOut
put
:Thenumber12321i
spal
i
ndr
ome

TestCases:
3

Sampl
eInput
:Ent
ert
hev
alue=1331.
25

Sampl
eOut
put
:Thenumber1331i
spal
i
ndr
ome

TestCondi
ti
on:
4

Checkwhet
hert
hegi
venwor
dispal
i
ndr
omeornot
.(Wr
it
eaC
Pr
ogr
am)

4.Gener
ateFi
bonacci
number
s.(
Edi
tpr
ogr
am t
orunt
het
estcases)

TestCases:
1

Sampl
eInput
:Ent
ert
henumber=-
5

Sampl
eOut
put
:Thenumberi
sinv
ali
d

TestCases:
2
Sampl
eInput
:Ent
ert
hev
alue=11.
5

Sampl
eOut
put
:Thenumberi
sinv
ali
d

TestCondi
ti
on:
3

Gener
ateNegat
iveFi
bonacci
Ser
ies

Sampl
eInput
:Ent
ert
hev
alueofn
5
-
3 2 - 1 1 0

5.Pr
intnumber
sfr
om 1t
o100wi
thoutusi
ngl
oops.

TestCondi
ti
on:
1

Pr
intnumber
sfr
om m t
on

Sampl
eInput :
Entert
heval
ueofm :
200
Ent
ert
hev alueofn:300
Sampl
eOut put:

200

201

300

TestCondi
ti
on:
2

Pr
intnumber
sfr
om m t
oni
nrev
erse

Sampl
eInput :
Entert
heval
ueofm :
200
Ent
ert
hev alueofn:300
Sampl
eOut put:
300
299
298
.
.
.
200

6.Checki
fagi
veny
eari
sal
eapy
earornot
.

TestCases:
1

Sampl
eInput
:Ent
ert
hey
ear=0

Sampl
eOut
put
:Ent
erVal
i
dYear

TestCases:
2

Sampl
eInput
:Ent
ert
hey
ear=1900

Sampl
eOut
put
:1900i
snotal
eapy
ear

TestCases:
3

Sampl
eInput
:Ent
ert
hey
ear=1600

Sampl
eOut
put
:1600i
sal
eapy
ear

TestCases:
4

Sampl
eInput
:Ent
ert
hey
ear=1532

Sampl
eOut
put
:1532i
sal
eapy
ear

TestCases:
5

Sampl
eInput
:Ent
ert
hey
ear=1951

Sampl
eOut
put
:1951i
snotal
eapy
ear

TestCases:
6

Sampl
eInput
:Ent
ert
hev
alue=16.
48

Sampl
eOut
put
:Ent
ert
hev
ali
dNumber

TestCondi
ti
on:
7

Wr
it
eapr
ogr
am t
opr
intnumberofday
sint
haty
ear

Sampl
eInput
:Ent
ert
hey
ear=1951

Sampl
eOut
put
:NumberofDay
s365

TestCondi
ti
on:
8
Wr
it
eapr
ogr
am t
opr
intnumberofday
sint
haty
ear

Sampl
eInput
:Ent
ert
hey
ear=1164

Sampl
eOut
put
:NumberofDay
s366

TestCondi
ti
on:
9

Wr
it
eapr
ogr
am t
opr
intt
hepr
evi
ousl
eapy
ear

Sampl
eInput
:Ent
ert
hey
ear=1951

Sampl
eOut
put
:Pr
evi
ousLeapYear=1948

TestCondi
ti
on:
10

Wr
it
eapr
ogr
am t
opr
intt
hepr
evi
ousl
eapy
ear

Sampl
eInput
:Ent
ert
hey
ear=1164

Sampl
eOut
put
:Pr
evi
ousLeapy
ear=1160

TestCondi
ti
on:
11

Wr
it
eapr
ogr
am t
opr
intt
hepr
evi
ousl
eapy
ear

Sampl
eInput
:Ent
ert
hey
ear=1904

Sampl
eOut
put
:Pr
evi
ousLeapYear=1896

TestCondi
ti
on:
12

Wr
it
eapr
ogr
am t
opr
intt
hepr
evi
ousl
eapy
ear

Sampl
eInput
:Ent
ert
hey
ear=4

Sampl
eOut
put
:Pr
evi
ousLeapy
ear=Nol
eapy
ear

You might also like