[go: up one dir, main page]

0% found this document useful (0 votes)
11 views11 pages

c programs

The document contains a series of C programming exercises designed for students at an autonomous institution under VTU. Each exercise includes a problem statement, sample code, and instructions for implementation, covering various programming concepts such as conditionals, loops, and mathematical calculations. The exercises range from simple tasks like determining the quadrant of an angle to more complex ones like calculating the area of a triangle.

Uploaded by

ssimmply862
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)
11 views11 pages

c programs

The document contains a series of C programming exercises designed for students at an autonomous institution under VTU. Each exercise includes a problem statement, sample code, and instructions for implementation, covering various programming concepts such as conditionals, loops, and mathematical calculations. The exercises range from simple tasks like determining the quadrant of an angle to more complex ones like calculating the area of a triangle.

Uploaded by

ssimmply862
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/ 11

B.N.

M.I
nst
it
uteofTechnol
ogy
AnAut
onomousI
nst
it
uti
onunderVTU
Depar
tmentofI
nfor
mat
ionSci
enceandEngi
neer
ing

1.Const
ructapr
ogr
am t
oreadanangl
efr
om t
heuseranddi
spl
ayi
tsquadr
ant
.

#include<st dio.
h>
voidmai n()
{
intangl e;
print f
(“Enterangl e[0-
360]:“)
;
scanf ( “
%d” ,
&angl e)
;
if(angl e>0&&angl e<90)
printf(“Theangl eisinfi
rstquadrant\n”
);
elsei f(angl
e>90&&angl e<180)
pr i
ntf
(“Theangl ei
sinsecondquadr ant\n”
);
elsei f(
angle>180&&angl e<270)
pri
ntf(“
Theangleisinthir
dquadrant\n”
);
elseprintf(
“Theangleisi
nf ourt
hquadrant\n)
;
}

2.Const
ructapr
ogr
am t
ofi
ndr
oot
sofquadr
ati
cequat
ion.

#include<st dio.h>
#include<mat h.h>
voidmai n()
{ floata, b,c,
x1,x2,
discr
;
pr i
ntf(“\nEnt ertheco-effi
cei
ntsoft
heequat
ion:“)
;
scanf (“%f %f %f”,
&a,&b,&c)
;
discr=b* b-4* a*c;
i
f(discr==0)
{pr i
ntf(“Root sar eequal\n”)
;
x1=- b/( 2.0*a);
printf(“Val ueofX1=X2=%f ”,
x1);
}
else
i
f( discr>0)
{
x1=( (-b+sqr t(
discr)
)/(2*
a));
x2=( (-
b-sqr t(
discr)
)/(2*
a));
printf(“Root sar edisti
nctandX1=%fandX2=%f \n”
,x1,
x2)
;
}
else{
discr=-discr
x1=-
b/(2*a)
;
x2=sqr t
(di
scr
)/(2*a)
;
pri
ntf
(“Root
sarei maj
inary\n”);
pri
ntf
(“X1=%d+i%d\ n”
,x1,x2)
;
pri
ntf
(“X1=%d- i%d\ n”,
x1,x2);
}
}

3aConstr
uctaprogr
am t
oreadt
hesi
desofat
ri
angl
eanddet
ermi
newhet
heri
tis
i
soscel
esornot.

#include<stdio.h>
voidmai n()
{
fl
oata, b,c;
printf
(“Enterthe3si desofatri
angle:“);
scanf(“%f%f%f ”,
&a,&b,&c)
;
i
f (
(a==b) |
|
(b==c)||
(c==a))
pr
intf(
“Thet r
iangleisi
soscel
es\ n”)
;
else
pr
intf(
“Thet r
iangleisnoti
sosceles\n”)
;
}

3bConst
ructapr
ogr
am t
ocheckwhet
hert
hegi
veny
eari
sal
eapy
earornot
.

#include<stdi
o.h>
voidmai n()
{intyear;
printf(
“Enteray earval
ue: “
);
scanf (“
%d” ,
&y ear)
;
if((
(year%4==0)&&( y
ear%100!=0))|
|(y
ear%400==0)
)
pri
ntf(
“%di saleapyear\n”,
year
);
else
pri
ntf(
“%di snotaleapyear\n”,
year)
;
}

4 Const ructaprogram t
oreadcustomercode&callsmadeandpr
intthebi
llwi
th
t
hegi v
encondi ti
on:
Thecostofmobi l
eservi
ceisRs.
250+Rs. 1.25f
oreachcal
lmadeoverandabove
100call
s.
#incl
ude<stdi
o. h>
voidmain()
{
i
ntcustcode, cal
ls;
fl
oatbil
l;
pri
ntf
(“Ent
ert hecustomercodeandno.ofcall
smade: “
);
scanf(“
%d%d”,&cust
code,&cal
ls);
i
f(call
s<100)
bil
l=250;
el
se
bi
ll=250+(call
s-100)*1.
25;
pri
ntf(
“Customercode:%d,Billamount=%f
”,cust
code,
bil
l
);
}

5.Constr
uctapr
ogr
am t
ofi
ndwhet
hert
hegi
vennumberi
soddorev
enusi
ng
swit
chstat
ement
.

#include<st dio.h>
voidmai n(
)
{intnum;
pr i
ntf(“Enternumber[ 1–10] :“)
;
scanf (
“%d” ,
&num) ;
swi tch(num)
{case1:
case3:
case5:
case7:
case9:pr intf
(“No.i
sodd\ n”)
;
break;
case2:
case4:
case6:
case8:
case10:pr i
ntf
(“No.i
sEven\ n”)
;
break;
def ault
: pri
nt f
(“I
nval
idi
nput\n”);
break;
}
}

6.Const
ructaprogram t
oreadav
alue[1-
7]andpri
nt‘
Monday
’ifi
nputi
s1,

Tuesday’i
finputi
s2andsoonusingSwitchSt
atement
.

#incl
ude<stdio.h>
voidmain()
{
intnum;
pri
ntf
(“Enterav al
uebetween1to7:
“);
scanf(“
%d” ,
&num) ;
swit
ch(num)
{
case1: pri
ntf
(“Monday\
n”)
;
break;
case2:print
f(“
Tuesday\ n”);
break;
case3:print
f(“
Wednesday\ n”)
;
break;
case4:print
f(“
Thur sday\ n”)
;
break;
case5:print
f(“
Friday\ n”)
;
break;
case6:print
f(“
Sat urday\n”);
break;
case7:print
f(“
Sunday\ n”);
break;
defaul
t:pri
ntf
(“Thei nputisnotv al
i
d\n”
);
break;
}
}

7a))Const
ructaprogr
am t
ofi
ndwhet
hert
hegi
vennumberi
soddorev
enusi
ng
condi
ti
onaloper
ator

#i
nclude<stdio.h>
voidmai n()
{
intnum, r
em;
pr i
ntf
(“\
nEnt eranumber:“
);
scanf(“%d”,&num);
rem=num %2;
(rem==0) ?
pri
ntf
(“No.i
sev
en\n”
):pr
int
f(“
No.i
sodd\
n”)
;
}

7b)Const ructapr ogram toadd2number


sanddispl
ayt
her
esul
t
#incl
ude<st dio.h>
i
ntmai n()
{
intx, y, z;
printf(
“Entertwonumber s:
“);
scanf (
“%d%d” ,&x,&y);
z=x+y ;
printf(
“\nThesum,%d+%d=%d\ n”,
x,y
,z);
return0;
}

8Const
ructapr
ogr
am t
ochecki
finputchar
act
eri
sav
owelusi
ngSwi
tchCase
#i
nclude<stdi
o.h>
i
ntmai n()
{
charch;
pri
ntf(
"I
nputaCharact
er:"
);
scanf(
"%c",
&ch);

switch(ch)
{
case' a'
:
case' A':
case' e'
:
case' E'
:
case' i
':
case' I'
:
case' o'
:
case' O':
case' u'
:
case' U':
pri
nt f(
"\n\
n%cisavowel
.\n\n"
,ch)
;
break;
default:
pri
nt f(
"%cisnotav
owel.
\n\n",
ch);
}

r
etur
n0;
}

9.Const
ructapr
ogr
am t
otakei
nputofv
ari
ousdat
aty
pesi
nC

#i
nclude<st
dio.
h>
i
ntmai n(
)
{
i
ntnum1, num2;
fl
oatfr
acti
on;
charchar
acter
;

pr
int
f("
Ent
ert
wonumber
snumber
\n"
);

scanf
("%d%i"
,&num1,&num2)
;
pri
ntf
("\
n\nThetwonumbersYouhav
eent
eredar
e%dand%i
\n\
n",
num1,
num2)
;

pri
ntf
("\
n\nEnteraDecimalnumber
\n")
;
scanf
("%f"
,&fr
action)
;
pri
ntf
("\
n\nThef l
oatorf
ract
iont
hatyouhav
eent
eredi
s%f
",f
ract
ion)
;
pri
ntf
("\
n\nEnt
eraChar act
er\n"
);
scanf("
%c",
&charact
er);
pri
ntf
("\
n\nThecharact
erthatyouhav
eent
eredi
s%c"
,char
act
er)
;
ret
urn0;
}

10a.Const
ructapr
ogr
am t
oswapt
wonumber
susi
ngbi
twi
seoper
ator
.

#include<stdi
o.h>
voidmai n()
{
intx=6, y=4;
x=x^ y;
y=x^ y;
x=x^ y;
printf
("x=%dandy=%d"
,x,
y);
}

10b. CProgram t of i
ndar eaandci r
cumf
erenceofaci
rcl
e
#include<stdi
o. h>
i
ntmai n(
)
{
i
ntr ad;
floatPI=3. 14, area,ci;
pr i
ntf("
\nEnterr adi
usofci r
cle:")
;
scanf ("
%d" ,
&r ad);
ar ea=PI*r ad*r ad;
pr i
ntf("
\nAreaofci rcle:%f",area);
ci =2*PI*r ad;
pr i
ntf("
\nCircumf erence:%f" ,
ci);
ret ur
n( 0)
;
}

11.
Const
ructapr
ogr
am t
oshowbasi
cAr
it
hmet
icOper
ati
ons

#i
nclude<stdi
o.h>
i
ntmai n(
)
{
i
nta,b,add,subt
ract
,mul
ti
ply
;
f
loatdiv
ide;

pri
ntf
("Ent
ert
wointeger
s:\
n")
;
scanf
("%d%d"
,&a,&b);

add=a+b;
subtr
act=a-
b;
multi
ply=a*
b;
div
ide=a/b;

pr
int
f("
\nAddit
ionofthenumber
s=%d\ n",
add);
pr
int
f("
Subtr
actionof2ndnumberfr
om 1st=%d\n",subtr
act
);
pr
int
f("
Multi
pli
cati
onofthenumbers=%d\n",mult
ipl
y);
pr
int
f("
Divi
ding1stnumberfr
om 2nd=%f\n",
divi
de);

r
etur
n0;
}

12.
Const
ructapr
ogr
am t
ofi
ndt
heAr
eaofTr
iangl
eusi
ngBaseandHei
ght

#i
ncl ude<stdio.h>
i
ntmai n()
{
i
nth, b;
fl
oatar ea;
pri
nt f("
\n\nEntert
heheightoftheTri
angl
e:"
);
scanf ("
%d" ,
&h);
pri
nt f("
\n\nEntert
hebaseoft heTri
angl
e:"
);
scanf ("
%d" ,
&b);
area=( h*
b)/(fl
oat
)2;
pri
nt f("
\n\n\nTheareaofthetri
angl
eis:%f
",ar
ea)
;
return0;
}
}

13a) Wr
it
eapr
ogr
am conv
ertt
emptf
rom °
Cto°
F

#incl
ude<stdi
o.h>
voidmain()
{ f
loatdegc,degf;
pri
ntf(
“Entertemper
atur
einCel
sius:
\n”)
;
scanf(
“%f ”
,&degc)
;
degf=( degc*9/5)+32;
pri
ntf(
“Thet emper
atur
einFahr
enhei
tis%f\
n”,
degf
);
}

13b) Wr
it
eapr
ogr
am t
oreadachar
act
erandpr
inti
tsASCI
Ival
ue

#i
ncl
ude<stdi
o.h>
i
ntmain()
{ charx;
pri
ntf
(“Ent
erancharact
er:“
);
scanf
(“%c”,&x)
;
pri
ntf
(“\nThechar
acteri
s%candi
tsASCI
Ival
uei
s%d\
n”,
x,x)
;
ret
urn0;
}

14a Wr
it
eapr
ogr
am t
oCal
cul
ateSi
mpl
eInt
erest

#i
nclude<stdi
o.h>
i
ntmai n(
)
{
fl
oatamount ,rate,t
ime, si;
pri
ntf("
\nEnterPri
ncipal Amount: ")
;
scanf("
%f",&amount );
pri
ntf("
\nEnterRateofI nterest:"
);
scanf("
%f",&rat
e);
pri
ntf("
\nEnterPeriodofTi me : ")
;
scanf("
%f",&ti
me) ;
si=(amount*r ate*t ime)/100;
pri
ntf("
\nSimpleInterest: %f",
si)
;
ret
urn(0);
}

14bWrit
eaPr ogr
am ToFi
ndAreaAndPer i
meterofRectangl
e
Ar
ea=lengt
h*br
eadth=>l*
b,Per
imeter=2*
(lengt
h+br eadt
h)=>2*(
l+b)
#i
ncl
ude<st
dio.
h>

i
ntmai
n()
{

i
ntl=10, b=10;
pri
ntf
("Areaofr
ectangl
eis:%d"
,l*b)
;
pri
ntf
("\nPer
imet
erofrect
angl
eis:%d",
2*(
l+b)
);
ret
urn0;
}

15.Wr i
teaPr ogram ToCheckNumberI
sPosi
ti
veOrNegat
ive
#incl
ude<st di
o.h>
intmain()
{
inta;
print
f("
EnterTheNumberYouWantToCheck:\n"
);
scanf("
%d" ,
&a);
if(
a<0)
{
print
f("
NumberIsNegat
ive:
\n"
);
}
elseif
(a>0)
{
print
f("
NumberI
sPossiti
ve:
\n"
);
}
else
{
print
f("
YouEnt
erZero:\
n");
}
retur
n0;
}

16. Wr
it
eaPr
ogr
am t
oFi
ndt
heSi
zeofi
nt,
float
,doubl
eandchar

#i
ncl
ude<st
dio.
h>

i
ntmai n()
{
i
ntintegerType;
charcharType;
fl
oatfloat
Type;
doubledoubleType;
print
f("Si
zeofintis:
%ld"
,si
zeof
(i
nteger
Type)
);

pr
int
f("
\nSi
zeofchari
s:%l
d",
sizeof
(char
Type)
);

pr
int
f("
\nSi
zeoff
loati
s:%l
d",
sizeof
(fl
oat
Type)
);

pr
int
f("
\nSi
zeofdoubl
eis:
%ld"
,si
zeof
(doubl
eTy
pe)
);

r
etur
n0;
}

17. Wr i
teaPr ogram t
oSwapTwoNumber
sUsi
ngaTempor
aryVar
iabl
e
#include<st dio.
h>
intmai n()
{
intx,y;
printf
("EnterValueofx");
scanf("%d" ,&x);
printf
("\nEnterValueofy")
;
scanf("%d" ,&y);
i
nttemp=x;
x=y ;
y=temp;

pri
ntf
("\
nAf
terSwappi
ng:
x=%d,
y=%d"
,x,
y);
ret
urn0;
}

18Wri
teaProgram t
ocomput
eCompoundInterest
t
for
mula:Amount=P(1+R/
100)CompoundInterest=Amount–P

#i
ncl
ude<mat
h.h>

i
ntmain()
{
doubl
epri
nci
pal
=10000;

doubl
erat
e=5;

doubl
eti
me=2;

doubl
eAmount=pri
ncipal
*
((
pow(
(1+rate/100)
,
ti
me)
));
doubl
eCI=Amount-pr
inci
pal
;

pri
ntf
("CompoundI
nter
esti
s:%l
f"
,CI
);
ret
urn0;
}

19.Writ
eaprogr am tocalcul
atet
hepar ki
ngchar
gesofav ehicl
e.Ent
ertypeofvehi
cle
as[cforcar
,bf orbus, tf
ortwowheeler]andt
heno.ofhours.Thecalcul
ati
onisfor
bus-
20Rs.Perhour ,forcarRs.
15/hourandfor2wheel
erRs.10perhourandRs. 7per
hourforal
lothertypes

#include<stdio.h>
voidmai n()
{charv eh_ t
ype;
inthour s,chrg;
printf
(“Enterthetypeofvehicl
eandhour
sofpar
king:
“);
scanf(“%c%d” ,&veh_t
ype,&hours)
;
veh_type=t olower(
veh_t
ype);
if(
veh_type==‘ b’
)
chrg=hour s*20;
elseif(
v eh_type==‘c’)
chrg=hour s*15;
el
seif(
v eh_
type==‘t
’)
chrg=hours*10;
el
sechr g=hour s*7;
pr
int
f(“Theparkingchar
gei
s%d\
n”,
chr
g);
}

20.I
mpl
ementasi
mpl
ecal
cul
atorusi
ngswi
tchst
atement

#include<st dio.h>
voidmai n()
{floatn1,n2, res;charop;i
nterr=0;
pr i
ntf(“
Ent ert wonumber s:“);
scanf (“%f%f ”,&n1,
&n2);
pr i
ntf(“
Ent ert heoperat
or:“);
scanf (
“%c” ,
&op) ;
swi tch(op)
{case‘ +’:r es=n1+n2;
break;
case‘ -
’:r es=n1-n2;
break;
case‘ *’
:res=n1* n2;
break;
case‘ /’
:if(n2==0)
err=1;
el
ser es=n1/ n2;
break;
default:printf(“I
nvali
doperat
or\ n”)
;
}
If
(err==0)
pri
nt f
(“Result=%f \
n”,
res);
elsepr i
ntf(“Dividebyzeroerror\ n”
);
}

You might also like