0% found this document useful (0 votes)
68 views

C++ Exercises

The document provides 14 questions asking to write C++ programs to perform various calculations including summing numbers, calculating averages, finding differences, modular arithmetic, solving equations, and calculating areas and circumferences of shapes. The programs are to use different loops and conditional statements like for, while, do-while, if-else. The last question is cut off asking to write a program to read a matrix with n*n elements and perform some operation on it.

Uploaded by

Hamza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

C++ Exercises

The document provides 14 questions asking to write C++ programs to perform various calculations including summing numbers, calculating averages, finding differences, modular arithmetic, solving equations, and calculating areas and circumferences of shapes. The programs are to use different loops and conditional statements like for, while, do-while, if-else. The last question is cut off asking to write a program to read a matrix with n*n elements and perform some operation on it.

Uploaded by

Hamza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Q1: writ

eapr ogram t
ocalculat
easum oft
wonumber
s:
#include<i
ostream.h>
#include<conio.
h>
voidmai n()
{i
ntx=10;
i
nty =20;
i
ntz ;
z=x+y ;
cout<<"theResultofzis:
\n\a";
cout<<z;
getch();
}

Q2:writ
eapr ogram tocalcul
ateasum oft
wonumber
sent
eredf
rom key
boar
d:
#incl
ude<iostr
eam. h>
#incl
ude<conio.h>
voidmain(){
intx;
i
nty ,
z;
ci
n>>x>>y ;
z=x+y;
cout<<"t
heResul tofzis:
\n\a"
;
cout<<z;
getch();
}

Q3: wr i
teaC++pr ogr am t
ocal cul
atethesum andav er
ageofsi
xstudentsdegr
ees
andpr i
nttheappr opr iateresul
tinthescreenusing(swit
ch)st
atement:
#include<iost r
eam. h>
#include<coni o.h>
voidmai n()
{i
nty ;
fl
oatx1, x2,x3,
x4,x5, x6,sum,av;
cout<<" Enter6integernumbery ouwant\ n"
;
cin>>x1>>x2>>x3>>x 4>>x5>>x6;
sum=x1+x2+x3+x4+x5+x6;
av=sum/ 6.0;
y=av/ 10;
cout<<sum<< " \\" << " \\" <<" TheSummat i
on\n";
cout<<av << "\\ "<< " \\" <<"TheAv er
age\n";
switch( y)
{case10, 9:cout<<" Excel l
ent\n";
break;
case8: cout <<"
Ver ygood\ n\ a"
;
break;
case7: cout <<"
Good\ n";
break;
case6: cout <<"
Medi um\ n"
;
break;
case5: cout <<"
Pass\ n";
1
break;
case4,3,
2,1:
cout
<<"
Fai
l
ed\
n";
break;
}
getch();
}

Q4: r
epeatt hesamepr ogram i
nQ3using(if
)st
atement
:
#include<i ost ream. h>
#include<coni o.h>
voidmai n()
{i
nty ;
fl
oatx1, x2,x3, x4, x5,x6,sum,av;
cout<<" Enter6f loatNumbery ouwant\n"
;
cin>>x1>>x2>>x3>>x 4>>x5>>x6;
sum=x1+x2+x3+x4+x5+x6;
av=sum/ 6;
y=av/ 10;
cout<<sum<<" ---
----
-"<<"Thesummat i
on\n";
cout<<av <<" ------
---"
<<" TheAv er
age\
n";
i
f(av>=0&&av <50)
cout<<" Failed\ n";
elsei f(
av >=50&&av <60)
cout<<" Pass\ n" ;
elsei f(
av >=60&&av <70)
cout<<" Medi um\ n";
elsei f(
av >=70&&av <80)
cout<<" Good\ n";
elsei f(
av >=80&&av <90)
cout<<" VeryGood\ n" ;
elsei f(
av >=90&&av <=100)
cout<<" Excel lent \n";
else
cout<<" TheAv erageOut putTheRange";
getch( )
;}

Q5: aprogram t
ocal cul
atet
hedif
fer
encebet
weent
wonumber
s:
#include<i
ostr
eam. h>
#include<coni
o.h>
voidmai n()
{i
ntx=100;
i
nty =20;
i
ntz ;
z=x-y;
cout<<"theResultofzis:
\n\
a";
cout<<z;
getch();
}

2
Q6: apr ogram t
odi vi
detwonumber
sandaddt
her
esul
ttoanot
hernumberandpr
int
theresul tt
oscreen:
#include<iostr
eam. h>
#include<conio.h>
voidmai n(
)
{i
ntx=100;
i
nty =20;
i
ntz =30;
i
ntw;
w=(x/ y)+z;
cout<<"theResul tofzis:
\n\
a";
cout<<w;
getch();}

Q7: aprogram t
ocal cul
atet
hemodoft
wonumber
sandout
putt
her
esul
t:
#include<i
ostr
eam. h>
#include<coni
o.h>
voidmai n()
{i
ntx=105;
i
nty =20;
i
ntz ;
z=x%y ;
cout<<"theResultofzis:
\n\
a";
cout<<z;
getch();
}

Q8: aprogram t
osol v
et heequat
ion:
z=(
x*y
)/w:
#include<i
ostr
eam. h>
#include<coni
o.h>
voidmai n()
{i
ntx=100;
i
nty =20;
i
ntw=50;
i
ntz ;
z=(x*y)/w;
cout<<"theResultofzis:\
n\a"
;
cout<<z;
getch();
}

Q9: aprogram t oreadtennumber


sandcal
cul
atet
hesum andav
erageoft
hem
using( f
or)statement:
#include<i
ostream. h>
#include<conio.h>
voidmai n(){
inti,x;
fl
oatav ;
i
ntsum=0;
3
cout<<"
Enter10IntegerNumber \n"
;
for(i
=1;i
<=10;
i++)
{ci
n>>x;
sum=sum+x; }
av=sum/ 10;
cout<<"
\nTheSummat ion="<<sum;
cout<<"
\nTheAv erage="<<av;
getch()
;}

Q10: writeaprogram i
nC++topri
ntthenumber
sbet
ween1and50t
hatar
e
divi
dabl eon3using(whil
e)st
atement:
#include<iost
ream.h>
#include<conio.
h>
voidmai n()
{i
ntj=1;
while(j<=50)
{i
f((j
%3) ==0)
cout<<j<<"\n"
;
j
++;}
getch();
}

Q11: apr ogram inC++topr


intt
henumber
sbet
ween1and10usi
ng(
Do-
Whi
l
e)
statement :
#include<iostr
eam. h>
#include<conio.h>
voidmai n()
{i
ntj=1;
do
{cout<<j<<"\
n";
j
++;}
while(j<=10);
getch();
}

Q12: aprogram inc++languagetocalcul


atethear
eaofaci
rcl
e:
#incl
ude<i ostr
eam.h>
voidmai n(){constfl
oatpi=3.14;
i
ntr ,
t;
fl
oatar ea;
cout<<"entertheradi
usoft heci
rcl
e\n";
ci
n>>r ;
area=pi *r*r;
cout<<"thear eaofthecir
cleis"
<<ar
ea<<"\n"
;
ci
n>>t ;
}

Q13:
aprogram t
ocalcul
atet
hear
eaandci
rcumf
erenceofat
ri
angl
e:
#i
ncl
ude<i
osream.h>
4
voidmai n(){intbase,hei
ght,a1,
a2,a3;
fl
oatarea, cir
cumf er
ence;
ci
n>>base>>hei ght>>a1>>a2>>a3;
area=(0.5)*base*hei ght;
ci
rcumf erence=a1+a2+a3;
cout<<"thear eaofthetri
angleis"
<<area<<" "
<<"
theci
rcumf
erenceoft
he
tr
iangleis"<<cir
cumf er
ence;
}

Q14: wr i
teac++pr ogr
am toreadamat r
ix(
arr
ay)wit
hn*nandfi
ndt
hel
argest
numberi ntheseconddiagonal.
#include<iostr
eam.h>
mai n()
{inti,
j
,a[
3][
3],
max;
for(i=0;i
<3;i
++)
for(j=0;j
<3;j
++)
cin>>a[i]
[j
];
for(i=0;i
<3;i
++)
for(j=0;j
<3;j
++)
i
f( i
+j==2)
{max=a[ i]
[2-
i]
;
for(i=0;i
<3;i
++)
for(j=0;j
<3;j
++)
{
if(a[i]
[2-i
]>max)
max=a[ i]
[2-
i]
;}}
cout <<"maximum numberi ntheseconddi
agonal
is"
<<max;}

Q15:apr ogram inC++t ocal culatethear


eaandcir
cumf
erenceofar
ect
angl
e:
#i
nclude<i ostr
eam. h>
main(){
i
ntlength, widt
h;
cout<<"l
engt h=" ;cin>>length;
cout<<"width=" ;cin>>wi dth;
cout<<"perimeter=" <<2*(l
ength+wi
dth)
<<"
\n"
;
cout<<"area=" <<lengt h*width;
}

Q16:apr ogram toreadfourdegr


eesandcal
cul
atet
hesum andav
erageoft
hem,
andprintit:
#incl
ude<i ostr
eam.h>
main(){
i
ntm1, m2, m3,m4;
cout<<"Entermarks\n"
;
cin>>m1>>m2>>m3>>m4;
cout<<"avg="<<(m1+m2+m3+m4) /4;
}

Q17:
apr
ogr
am t
oreadaFahr
enhei
tdegr
eeandconv
erti
ttoCel
si
usdegr
eeusi
ng

5
theconver
sionequat i
on:c=(f
-32)
*5/
9
#incl
ude<iostream. h>
main(){ f l
oatf ;
cout
<<"f=" ;
cin>>f;
cout<<"
c=" <<(f-32)*5.
0/9.0;
}

Q18:wr i
teapr ogram t oreadaCelsi
usdegreeandconv
erti
ttoFahr
enhei
tdegr
ee
temperatureusingt heequat i
on:f
=c*9/
5+32:
#incl
ude<i ostr
eam. h>
main(){ f loatc;
cout<<"c=" ;cin>>c;
cout<<"f="<<c* 9.
0/5.
0+32;
}

Q19:
apr
ogr
am t
ocal
cul
ateaandbf
rom t
heequat
ions: ‫ﻭ‬

#i
nclude<iostr
eam. h>
#i
nclude<mat h.
h>
main(){ f loatw,x,y,z;
cout
<<"Enterw, x,y,
z\n"
;
ci
n>>w>>x>>y >>z;
cout
<<"a=\ n"<<( (
5+x)/z)+(y
/2.7*w)
;
cout
<<"b=" <<( 4.5*pow(x+2.
3*y,
2))/(
z+w)
;}

Q20:wr it
eapr ogram tor eadar eal
numberandpr
int(
posi
ti
ve)wheni
tisgr
eat
er
thanzeroandnegat iv
eot herwise:
#incl
ude<i ostr
eam. h>
main(){ f loatn;
cout<<"n=" ;
ci
n>>n;
if
(n>=0)cout <<"Positi
ve";
else cout<<"Negat iv
e";}

Q21: aprogr
am toreadt
wonumber
sandexchanget
hem i
fthef
ir
stonei
sgr
eat
er
thant hesecondone:
#include<iostr
eam.h>
mai n(){ i nta,b,
t;
cout <<"
a=" ;
cin>>a;
cout <<"
b=" ;
cin>>b;

6
i
f(a>b){
t=a;
a=b;
b=t;}
cout<<a<<""
<<b;
}

Q22:apr ogram toreadanumberandpr


inti
tst
ype(
evenorodd)
:
#i
nclude<i ostr
eam.h>
main(){ i ntn;
cout<<"n=";cin>>n;
i
f(n%2! =0)cout<<"Odd"
;
el
se cout <<"
Ev en";
}

Q23:
wri
teapr
ogr
am t
ocal
cul
ate(
y)f
rom t
heequat
ion:
y=

#i
nclude<i ostr
eam. h>
#i
nclude<mat h.
h>
main(){ f loatx,y;
cout<<"x=" ;cin>>x;
i
f(x>=0)y=5- pow( x,2)
;
i
f(x<0)y=2* pow( x,3);
cout<<"y=" <<y ;
}

Q24:apr ogram toreadtwonumber


sandpr
intt
hev
alueoft
hegr
eat
erone:
#i
nclude<i ostr
eam.h>
main(){ i nta,b;
cout<<"a=" ;
cin>>a;
cout<<"b=" ;
cin>>b;
i
f(a>b)cout<<a;
el
secout <<b;}

Q25:wr i
teapr ogram t oreadt
wonumber
sandanoper
ati
ont
oappl
yitont
hem:
#i
nclude<i ostream. h>
main(){ f loata, b;
chars;
cout<<"a, s,
b\ n";
ci
n>>a>>s>>b;
i
f(s=='+')cout <<a+b;
i
f(s=='-
')cout <<a-b;
i
f(s=='*'
)cout <<a* b;
i
f(s=='/'
)cout <<a/ b;
}

7
Q26:
apr
ogr
am t
ocal
cul
ateyf
rom t
heequat
ion:
y=

#i
nclude<iostream.h>
#i
nclude<mat h.h>
main(){ i ntx, y
;
cout
<<"x=" ;ci
n>>x;
swit
ch(x) {
case-5:y=3*x-
7;break;
case2:
case5: y=5*pow(x,
2);br
eak;

case-
4:
case4:y
=x-4*
pow(
x,3)
;br
eak; }
cout
<<"
y="<<y;
}

Q27:
wr i
teapr ogr am tocal
cul
atet
hef
act
ori
alofanumberaf
terr
eadi
ngi
t:
#i
nclude<i ostr eam.h>
main()
{i ntn, f =1;
cout<<"n=" ; ci
n>>n;
for(
inti=0; i<n;i++)
f* =( n- i
);
cout<<"f=" <<f ;
}

Q28:
wr i
teapr ogr
am t ocal
cul
atet
hesummat
ionoft
henumber
s:4,
4.5,
5,…,
9.5,
10
#i
nclude<iostr
eam. h>
main()
{f l
oatn=4, sum=0;
do{ sum +=n;
n+=0. 5;
}while(
n<=10);
cout<<"
sum =" <<sum;}

Q29:
wr i
teapr ogram tocalcul
atet
hesum andav
erageof(
n)number
s:
#i
nclude<i ostream.h>
main()
{i ntn, sum =0, x;
cout<<"n=" ;ci
n>>n;
for(
inti=1; i
<=n; i
++)
{cout<<"x"<<i<<"=";
cin>>x;
sum +=x;}
cout<<"av g=" <<sum/ n;
}

8
Q30:
apr ogram t or ead( n)numbersandpr
intt
hemaxi
mum oneamongt
hem:
#i
nclude<i ostream. h>
main()
{i ntn, max, x;
cout<<"n=" ; cin>>n;
cout<<"x1=" ;cin>>x;
max=x;
for(
inti=2; i
<=n; i++)
{cout<<"x"<<i<<"=" ;ci
n>>x;
i
f(x>max)max=x; }
cout<<"max=" <<max; }

Q31:
apr ogram t or ead( n)numbersandpr
intt
hemi
nimum oneamongt
hem:
#i
nclude<i ostream. h>
main()
{intn,mi n, x;
cout<<"n=" ; cin>>n;
cout<<"x1=" ;cin>>x;
min=x;
for(
inti=2; i
<=n; i++)
{cout<<"x"<<i<<"=" ;ci
n>>x;
if
(x<mi n)mi n=x; }
cout<<"mi n=" <<mi n;
}

Q32:wri
teaprogram t
ocal
cul
atet
hedi
stancebet
weent
wopoi
ntsusi
ngt
he
fol
l
owingequati
on:d=

#i
nclude<i ost
ream. h>
#i
nclude<mat h.h>
main(){floatax, ay,az,bx,
by,bz;
cout<<"Enterax, ay,az\n";
cin>>ax>>ay >>az;
cout<<"Enterbx, by,bz\n";
cin>>bx>>by >>bz;
cout<<"d=" <<
sqrt(
pow( ax- bx,
2)+pow(ay-
by,
2)+pow(
az-
bz,
2))
;}

Q33:
wr it
eapr ogr am toreadatri
anglethreesi
desandpr
int"equi
l
at er
al"whenal
l
t
hesidesareequal andpr i
nt"
isoscel
es"whentwosidesareequalandpr i
nt
"
scal
ene"whenal l t
hesidesarediff
erent:
#i
nclude<iostream. h>
#i
nclude<mat h.h>
main(){fl
oatL1, L2,L3;
cout<<"EnterL1, L2,L3\n";
cin>>L1>>L2>>L3;
9
i
f(
L1==L2&&L1==L3&&L2==L3)cout<<"Equil
ater
al"
;
el
se
i
f(
L1==L2||
L1==L3|
|L2==L3)cout
<<"I
sosceles";
el
se
i
f(
L1!=L2&&L1!
=L3&&L2!=L3)cout
<<"Scalene";
}

Q34: writ
eaprogr
am t
ocal
cul
atethear
eaofatr
iangl
eusi
ngthef
oll
owi
ng
equations:
Area= asL1,L2,L3ar
ethetri
angl
esi
desand
s=(L1+L2+L3)
/2

#i
nclude<iostream.h>
#i
nclude<mat h.
h>
main(){fl
oatL1, L2,L3,s;
cout<<"EnterL1,L2,L3\n"
;
cin>>L1>>L2>>L3;
s=(L1+L2+L3) /2;
cout<<"area=<<sqr t(
s*(
s-L1)
*(s-
L2)
*(s-
L3)
);
}

Q35: toshowt hest ar


sinthefi
gurebel
ow:/ /*
#include<i ostream. h> /
/ **
voidmai n(
) // ***
{ // ****
for(
inti=1;i<=4;i++)
{f or(i
ntj=1; j
<=i;
j++)
cout <<"*";
cout <<endl ;}}
-
---
---
----
--
---
----
--
---
--
--
--
--
Q36: toshowt hestarsint
hefigur
ebelow:
#include<i ostream. h> // ****
voidmai n(
) /
/ ***
{ /
/ **
for(
inti=4;i>=1;i--
) /
/*
{
for(i
ntj=1; j
<=i;j
++)
cout <<"*";
cout <<endl ;}}
-
---
---
----
--
---
----
--
---
--
-
Q37: toshowt hestarsint
hefigur
ebelow:
#include<i ostream. h>
voidmai n(
)
{ /
/ *
for(
inti=1;i<=4;i++) /
/ **
{ /
/ ***

10
for(
intj =1;j
<=4-i
;j
++) //****
cout<<"";
for(
intk=1; k<=i;
k++)
cout<<"*"
;
cout <<endl;}}
-
---
---
----
---
--
---
--
--
--
--
Q38: toshowt hestarsint
hef i
gurebel
ow:
#include<i ostr
eam. h> / / ****
voidmai n() /
/ ***
{ /
/ **
for(
inti=4;i>=1;i
--
) // *
{
for(
intj =1;j
<=4-i
;j
++)
cout<<"";
for(
intk=1; k<=i;
k++)
cout<<"*
";
cout <<endl;}}
-
---
---
----
---
--
---
--
--
-

Q39: toshowt hest arsinthefigur


ebelow:
#i nclude<i ost ream. h> /
/ *
voidmai n() // ***
{ // *****
inti,j
,k,x=3,y =1; // *******
for (
i=1;i<=7; i
++) // *****
{ // ***
for(j
=1; j<=x;j
++)/ / *
cout <<"";
for(k=1; k<=y;k++)
cout <<"*";
cout <<endl ;
i
f(i<4)
{ x--;
y=y +2; }
else
{ x++;
y-=2; }}}
-
-----
--
---
----
---
----
---
---
--
----
--
--
Q40: toshowt henumber sinthefigur
ebelow:
#i nclude<i ost ream. h>
voidmai n()
{inti,j
; // 234567
for (
i=2;i<=7; i
++) /
/345678
{ // 456789
for(j
=i ;
j<=i+5;j++) // 5678910
cout <<j<<"";/ / 67891011
11
cout
<<endl
; /
/ 789101112
}}
-
-----
--
---
----
----
------
---
--
----
--
Q41: toshowt henumber si
nthefigurebel
ow:
#include<i ost ream. h> //1
voidmai n() // 21
{ // 321
for(i
nti =1;i<=4; i++) // 4321
{
for(i
ntj =i;j
>=1; j
--)
cout <<j ;
cout <<endl ;} }
-
-----
--
---
----
----
------
---
-
Q42: toshowt henumber si
nthefigurebel
ow:
#i nclude<i ost ream. h>
voidmai n(){i nti ,j
,
k,x=2,y=1;
for (
i=1;i<=5; i
++)
{ for(j
=1; j<=x; j
++)
cout <<"";
for(k=1; k<=y ;k++)
cout <<y;
cout <<endl ;
i
f(i<3)
{ x- -; 1
y=y +2; } 333
else 55555
{ x++; 333
y-=2; }}} 1
-
-----
--
---
----
----
------
--
Q43: whati st heout putofthefol
lowingprogr
am?
#include<i ost ream. h>
voidmai n()
{
for(i
nti =4;i>=1; i--)
{f or(intj=i;j>=1; j
--)
cout <<j ;
cout <<endl ;} }
-
-----
--
---
----
----
--
Q44: whati st heout putofthefol
lowingprogr
am?
#include<i ost ream. h>
voidmai n()
{for(i
nti =4;i>=1; i--
)
{f or(intj=1; j<=i ;
j
++)
cout <<j ;
cout <<endl ;} }

12
Q45: writeapr ogram t osolv
ethef
oll
owi
ngser
ies:
s=(x+x* 2/
2+x* 3/
3+x*4/4
+....
+x* n/n)
#i nclude<i ost ream. h>
voi dmai n(){i ntj,
x,p,n;
doubl ei,s=0. 0;
cout <<"Enterxv alue\ t"
;
cin>>x;
cout <<"Enternv alue\ t
";
cin>>n;
for(i
=1;i<=n; i
++)
{p=1;
for (
j=1;j
<=i;j++)
p=p* x;
s=s+p/ i;}
cout <<"Thesum =\ t" <<s;
}
---
---
----
--
---
---
-----
Q46: writeapr ogram t osolv
ethef
oll
owi
ngser
ies:
s=(x*
2/2!+x*4/
4!+x*6/
6!+.
..
.+
x* n/n!)
#i nclude<i ost ream. h>
voi dmai n(){i ntj,
x,k,i
,cp=2,cf
=2,
n;
doubl ef ,
p,s=0. 0;
cout <<"Enterxv alue\ t"
;
cin>>x;
cout <<"Enternv alue\ t";
cin>>n;
for(i
=1;i<=n; i
++)
{p=1;
f=1;
for(j
=1; j<=cp;j++)
p=p* x;
for(k=1; k<=cf ;
k++)
f=f*k;
s=s+p/ f;
cp+=2;
cf+=2; }
cout <<"Thesum =\ t" <<s;
}
---
---
----
--
---
---
----
Q47: writeapr ogram t osolv
ethef
oll
owi
ngser
ies:
s=(x*
1/2!+x*3/
4!+x*5/
6!+.
..
.+
x* a/(a+1) !
)
#i nclude<i ost ream. h>
voi dmai n(){i ntj,
x,k,i
,cp=1,cf
=2,
n;
doubl ef ,
p,s=0. 0;
cout <<"Enterxv alue\ t"
;
cin>>x;
cout <<"Enternv alue\ t";
cin>>n;
13
for(i
=1;i<=n; i
++)
{ p=1;
f=1;
for(j
=1; j<=cp;j++)
p=p* x;
for(k=1; k<=cf ;
k++)
f=f*k;
s=s+p/ f;
cp+=2;
cf+=2; }
cout <<"Thesum =\ t" <<s;
}
---
---
----
--
---
---
-----
--
Q48: writeapr ogram t osolv
ethefoll
owi
ngser
ies:
s=(x*
1/2!+x*
3/4!+x*5/
8!+
x* 7/16!+. .
..+x *11/ 64!).
#i nclude<i ost ream. h>
voi dmai n(){i ntj,
x,k,i
,cp=1,cf
=2;
doubl ef ,
p,s=0. 0;
cout <<"Enterxv alue\ t"
;
cin>>x;
for(i
=1;i<=6; i
++)
{ p=1;
f=1;
for(j
=1; j<=cp;j++)
p=p* x;
for(k=1; k<=cf ;
k++)
f=f*k;
s=s+f /p;
cp+=2;
cf*=2; }
cout <<"Thesum =\ t" <<s;
}
---
---
----
--
---
---
-----
---
--
---
---
Q50: writeapr ogram t osolv
ethefoll
owi
ngser
ies:
s=(2!/
x*2–x*4/4!+8!/
x*8–
x* 16/ 16!+. ..
.-x*64/ 64! )
#i nclude<i ost ream. h>
voi dmai n(){i ntj,
x,k,i
,cp=2,cf
=2,
c=1,m=1;
doubl ef ,
p,s=0. 0;
cout <<"Enterxv alue\ t"
;
cin>>x;
for(i
=1;i<=6; i
++)
{ p=1;
f=1;
for(j
=1; j<=cp;j++)
p=p* x;
for(k=1; k<=cf ;
k++)
f=f*k;
if
(c%2==1)
14
s=s+m*f/
p;
el
se
s=s+m*p/f
;
cp*=2;
cf*
=2;
m*=-1;
c++;}
cout
<<"
Thesum =\t"
<<s;
}

Q51: wr i
t eaprogram tosol
vethefoll
owingser
ies:
s=2!
/x2-x4/
4!+8!
/x8–x16/
16!
64
+..
..
-x / 64!.
#include<i ostream.h>
voidmai n()
{IntX,N,M,T,P;
Cin>>x;
N=2;
For(inti =1;i
<=16; i
++)
{ T=1;
For(
intK=1; k<=N ;k++)
T=T*K;
P=1;
M=N* 2;
For(
intj=1;j
<=M ; j
++)
P=p*j;

S=s+(
T/pow(
X,N)–pow(
X,M)
/P)
;
N=N*4;}

Q52:
Wr i
teaProgram t
hatdoest hefoll
owing:
-
- Promot est
heusertoent er5numbers.
- Pri
nttheNumber s.
- Pri
ntthesum andav erageofthesefi
venumber
s.
#i
ncl
ude<i
ostream.h>

v
oidmai
n()
{i
nta,
b,
c,
d,
e,
sum,
avg;

cout
<<"
Ent
ert
hef
ivenumber
s:"
;

ci
n>>a>>b>>c>>d>>e;

cout
<<"
thenumber
syouhav
eent
eredar
e:"
;

cout
<<a<<""
<<b<<""
;

cout
<<c<<""
<<d<<""
<<e<<endl
;

sum =a+b+c+d+e;
15
av
g=sum/
5;

cout
<<"
thesum i
s"<<sum;

cout
<<"andt
heav
eragei
s"<<av
g<<endl
;
}

Q53:Wr i
teaProgram t
hatpromot
est
heusert
oent
ert
hel
engt
handwi
dthofa
r
ectangleandprinti
tsar
ea.
#incl
ude<iostr
eam.h>

v
oidmai
n()
{doubl
elengt
h,wi
dth;

cout
<<"
Ent
ert
heLengt
hant
heWi
dthoft
her
ect
angl
e:"
;

ci
n>>l
engt
h>>wi
dth;

cout
<<"t
hear eaofther
ect
angl
eis
"
<<l
ength*
width<<endl
;}

Q54:
Wr i
teaProgram t
ofindt
heabsol
utev
alueofani
nteger
.
#i
ncl
ude<iostr
eam.h>

v
oidmai
n()
{i
nta;

cout
<<"
ent
eranumber
:"
;

ci
n>>a;

i
f(
a<0)

a*
=-1;

cout
<<"
theabsol
utev
alueoft
henumberi
s:"
<<a<<endl
;
}

Q55:
Wr i
teaProgram t
hatr
eadst
wodi
ff
erenti
nteger
sandpr
intt
hel
argest
.
#i
ncl
ude<iostr
eam.h>

v
oidmai
n()
{i
nta,
b;

cout
<<"
ent
ert
wonumber
s:"
;

ci
n>>a>>b;

i
f(
a>b)

cout
<<a<<"i
sthel
argest
"<<endl
;

16
el
se

cout
<<b<<"i
sthel
argest
"<<endl
;
}

Q56:
Wr i
teaProgram t
hatr
eadsoper
ati
onwi
thi
t'
soper
andsandt
henpr
intt
he
r
esul
t.
#i
ncl
ude<iostr
eam.h>

v
oidmai
n()
{doubl
ea,
b;

charx;

cout
<<"
ent
ert
heoper
ati
on:
";

ci
n>>a>>x>>b;

i
f(
x=='
+'
)

cout
<<a+b<<endl
;

el
sei
f(x=='
-
')

cout
<<a-
b<<endl
;

el
sei
f(x=='
*'
)

cout
<<a*
b<<endl
;

el
sei
f(x=='
/'
)

cout
<<a/
b<<endl
;

el
se

cout
<<"
Unknownoper
ati
on"
<<endl
;
}

Q57:Wri
teaPr ogram t
oreadacharact
eri
fit
'sinalphabetthenfi
ndi
fit
suppercase
orl
owercase.I
fit’
sadigitpr
intt
hati
tisadi
git.El
seprintthati
t’
saspeci
alchar
acter
.
#i
nclude<iostr
eam.h>

v
oidmai
n()
{charx;

cout
<<"
ent
ert
hechar
:"
;

ci
n>>x;

i
f(
x>='
A'&&x<='
Z'
)

cout
<<x<<"i
sUpper
casechar
"<<endl
;
17
el
sei
f(
x>='
a'&&x<='
z'
)

cout
<<x<<"i
sLower
casechar
"<<endl
;

el
sei
f(
x>='
0'&&x<='
9'
)

cout
<<x<<"i
saDi
git
"<<endl
;

el
se

cout
<<x<<"i
saspeci
alchar
"<<endl
;
}

Q58:
Wr i
teal
ooptopri
ntthenumber
sfr
om 1t
o10
#i
ncl
ude<i
ost
ream.h>

v
oidmai
n()
{ i
nti
=0;

whi
l
e(i
<=10)

cout
<<i
++<<endl
;}

Q59:
Wr i
teaProgram t
opri
ntt
hemul
ti
pleofnumber5bet
ween0t
o20.
#i
ncl
ude<iostr
eam.h>

v
oidmai
n()
{ i
nti
=5;

whi
l
e(i
<=20)

{
cout
<<i
<<endl
;

i
+=5;
}}

Q60:
Wr i
teal
ooptofi
ndt
hesum ofnumber
sfr
om 1t
o20.
#i
ncl
ude<i
ost
ream.
h>

v
oidmai
n()
{i
ntsum=0,
i=0;

whi
l
e(i
<=20)

sum +=i
++;

cout
<<"
thesum i
s:"
<<sum<<endl
;
}

Q61:
Wr i
teal
ooptofi
ndt
hesum ofoddnumber
sfr
om 20t
o300.
#i
ncl
ude<i
ost
ream.
h>

18
v
oidmai
n()
{i
nti
=20,
sum =0;

whi
l
e(i
<=300)

{
if
(i
%2==1)

sum +=i
;

i
++; }

cout
<<"
thesum i
s:"
<<sum<<endl
;
}

Q62:
Findthesum ofevennumberf
rom 1t
o30.
#i
ncl
ude<iost
ream.h>

v
oidmai
n()
{i
nti
=1,
sum =0;

whi
l
e(i
<=30)

{
if
(i
%2==0)

sum +=i
;

i
++;
}

cout
<<"
thesum i
s:"
<<sum<<endl
;
}

Q63:
Wr i
teal
ooptofi
ndf
actor
ial
sforanent
erednumber
.
#i
ncl
ude<i
ost
ream.
h>

v
oidmai
n()
{i
ntx,
y;

cout
<<"
ent
ert
henumber
:"
;

ci
n>>x;

y
=x;

whi
l
e(x!
=0)

{
if
(y%x==0)

cout
<<x<<endl
;

x-
-;
}}

Q64:
Findthemaxi
mum bet
ween20number
s.
#i
ncl
ude<i
ostr
eam.
h>

19
v
oidmai
n()
{i
ntx,
y;

cout
<<"
ent
ert
henumber
:"
;

ci
n>>x;

i
nti
=0;

whi
l
e(i
<=20)

{
if
(x>y
)

{
y=x;

cout
<<"
theMaxi
mum unt
ilnowi
s"<<y
<<endl
;
}

ci
n>>x;

i
++;
}}

Q65:
Wr i
teaforlooptopri
ntnumber1t
o40each5onl
i
ne.
#i
ncl
ude<iostr
eam.h>

#i
ncl
ude<i
omani
p.h>

v
oidmai
n()
{

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

{
cout
<<set
w(3)
<<i
;

i
f(
i%5==0)

cout
<<endl
;}
}

Q66:
Wr i
teaPr ogram t
hatreadsanumberandperfor
mthef
oll
owi
ng
- printthenumberdi gi
tsi
nr ever
seor
der
.
- Findt hesum ofit
'sdigi
ts.
- Findt heaverageofit
'sdigi
ts.
#i
ncl
ude<iostream>

#i
ncl
ude<i
omani
p>

usi
ngnamespacest
d;

v
oidmai
n()
{i
ntx,
y,
z=0,
avg=0,
sum=0;

ci
n>>x;
20
whi
l
e(x!
=0)

{
y=x%10;

x/
=10;

cout
<<y
<<endl
;

sum +=y
;

z++;}

av
g=sum/
z;

cout
<<"
thesum i
s"<<sum<<endl
;

cout
<<"
theav
gis"
<<av
g<<endl
;
}

Q67:
Wr i
teaProgram t
or eadasetofnon-
zer
o(whenr
eadzer
o,st
opt
hepr
ogr
am)
andf
ind:
- sum
- average
- maxi mum val
ue
- minimum value
-t henumberofv al
ues
- sum ofnumber sthatdi
vi
deon5.
#i
ncl
ude<i
ostream>

#i
ncl
ude<i
omani
p>

usi
ngnamespacest
d;

v
oidmai
n()
{i
ntx,
y,
max,
min,
z=0,
avg=0,
sum=0,
sum5=0;

ci
n>>x;

max=x;

mi
n=x;

whi
l
e(x!
=0)

{
if
(x>max)

max=x;

i
f(
x<mi
n)

mi
n=x;
21
sum+=x;

z++;

i
f(
x%5==0)

sum5+=x;

ci
n>>x;
}

av
g=sum/
z;

cout
<<"
thesum i
s"<<sum<<endl
;

cout
<<"
theav
eragei
s"<<av
g<<endl
;

cout
<<"
themax
imum v
aluei
s"<<max<<endl
;

cout
<<"
themi
nimum v
aluei
s"<<mi
n<<endl
;

cout
<<"
thenumber
sofv
aluesi
s"<<z<<endl
;

cout
<<"t
hesum ofnumber
sthatdi
vi
deon5i
s
"
<<sum5<<endl
;}

Q68: usingfuncti
ons, wr
it
eapr ogr
am t
oreadani
ntegerarr
aywith5el
ements
andpr i
ntt heevennumber sandoddnumbersofi
t:(
repeatall
thepr
evi
ousfor
secondar r
ay)
#include<i ostr
eam.h>
read(intar[]){
cout<<"Enter5number s:
";
for
(inti=0;i
<=4;i++)
ci
n>>ar[
i]
;
}

i
sEv
en(i
ntx){
i
f(
x%2==0)r
etur
n1;
r
etur
n0;}

i
sOdd(
intx){
r
eturn(
!i
sEv
en(
x))
;}

pr
int
Even(
intar[
]){
cout<<"
Evennumber s:
";
for
(i
nti=0;i<=4;i
++)
if
(i
sEv en(
ar[
i]
))cout
<<ar
[i
]<<""
;

22
cout
<<endl
;
}

pr
int
Odd(i
ntar []
){
cout<<"Oddnumber s:
";
for
(i
nti=0; i
<=4;i
++)
if
(isOdd(ar
[i
])
)cout
<<ar
[i
]<<""
;
cout<<endl;
}

mai
n(){inta[5];
read( a)
;
printEven(a)
;
printOdd(a);

i
ntb[ 5]
;
read(b)
;
print
Even(b)
;
print
Odd(b);
}

Q69: usingf uncti


ons,writ
eaprogr
am toreadani
ntegerarr
aywith5element
s
andpr inttheposi t
ivenumbersandnegat
ivenumbersofit
:(r
epeatal
lthe
previousf orsecondar r
ay)
#include<i ostream.h>
read(intar[]){
cout<<"Enter5number s:"
;
for(
inti=0;i<=4;i
++)
ci
n>>ar[i
];
}

i
sPosi
ti
ve(i
ntx){
i
f(x>=0)ret
urn1;
r
eturn0;
}

i
sNegati
ve(
intx){
ret
urn(!
isPosi
ti
ve(
x))
;}

pr
int
Posit
ive(i
ntar[]
){
cout<<"Posit
ivenumber s:
";
for
(i
nti=0; i
<=4;i++)
if
(isPosi
tiv
e(ar[
i]
))cout
<<ar
[i
]<<""
;
cout<<endl;
}

pr
int
Negative(i
ntar[
]){
cout<<"Negati
venumber s:
";
for
(i
nti =0;i
<=4;i
++)
if(i
sNegati
ve(
ar[
i]
))cout
<<ar
[i
]<<""
;
cout<<endl;
}

23
mai
n(){inta[5]
;
read(a);
printPosi
ti
ve(a);
printNegati
ve(a)
;

i
ntb[ 5]
;
read(b)
;
print
Posit
ive(b);
print
Negati
v e(b)
;}

Q70:usingf
unct
ions,
wri
teapr
ogr
am tor
eadani ntegerar
raywit
h5elementsand
pr
intthepri
meandnotpri
menumber
sofit:
(repeatallt
heprevi
ousf
orsecond
ar
ray):

#i
ncl
ude<i
ost
ream.h>
#i
ncl
ude<mat
h.h>

r
ead(
intar[]
){
cout<<"Enter5numbers:
";
for
(i
nti=0; i
<=4;i++)
cin>>ar[
i]
;
}

i
sPr
imary(
intx){
f
or(i
nti=2;i
<=sqr
t(x);
i++)
i
f(x%i==0)r et
urn0;
r
etur
n1; }

i
sNot
Primary
(intx){
r
etur
n(!i
sPr i
mary
(x)
);
}

pr
int
Primary(
intar[
]){
cout<<"Pri
marynumber s:
";
for(
inti
=0; i
<=4;
i++)
if
(isPr
imary(
ar[
i]
))cout
<<ar
[i
]<<""
;
cout<<endl;
}

pr
int
NotPrimar y
(intar
[]){
cout<<"NotPr i
mar ynumbers:"
;
for
(i
nti=0; i
<=4;i++)
if
(isNotPri
mar y(
ar[
i]
))cout
<<ar
[i
]<<""
;
cout<<endl;}

mai
n(){inta[5]
;
read(a);
printPr
imary(
a);
printNotPr
imary
(a)
;
24
i
ntb[ 5]
;
read(b)
;
print
Pri
mary(
b);
print
NotPr
imary
(b)
;}

Q71: usingf unct i


ons,readt wo(5-real
elements)ar
ray
sandpr
intt
heresul
toft
he
addition,subt racti
on,mul t
ipli
cati
on,anddivi
si
onofeachel
ementwit
hthe
correspondi ngel ementi nthesecondar ray
:
#include<i ostream. h>
read(floatar [
]){
cout<<"Ent er5number s:
";
for(
inti=0; i
<=4; i
++)
ci
n>>ar [
i];
}

cal
c(f
loata[]
,charp, fl
oatb[]
, f
loatc[
]){
for(i
nti=0;i<=4;i
++){
if
( p=='+'
)c[i
]=a[
i]
+b[i]
;
if
( p=='-
')c[
i]
=a[i
]-
b[i
];
if
( p=='*'
)c[
i]
=a[i
]*b[i
];
if
( p=='/'
)c[
i]
=a[i
]/b[
i]; }}

pr
int
(char*msg, f
loatar[
]){
cout<<msg;
for(
inti
=0;i<=4;i
++)
cout<<ar[
i]
<<"";
cout<<endl;
}

mai
n(){ f
loata[
5],
b[5]
,c[
5];

r
ead(
a);
r
ead(
b);

cal
c(a,'
+'
,b,c)
;
pri
nt(
"Addi
ti
on:",
c);

cal
c(a,'
-'
,b,c)
;
pri
nt(
"Subtr
acti
on:
",c)
;

cal
c(a,'
*'
,b,c);
pri
nt(
"Multi
ply
:",
c);

cal
c(a,'
/'
,b,c)
;
pri
nt(
"Di
ving:"
,c)
;}

Q72:
usi
ngf
unct
ions,
readt
woi
ntegerar
ray
sandcal
cul
atet
heav
erageofeachone,
25
thesum ofeachar r
ayelement
s,themi
nandmaxv
aluesf
oreachoneoft
hem:
#incl
ude<i ostr
eam.h>
read(
intar[]){
cout<<"Enter7numbers:
";
for
(i
nti =0;i
<=6;i++)
ci
n>>ar[
i]
;}

get
Max(i
ntar[
],
int&max){
max=ar [
0];
f
or(i
nti
=1;i<=6;
i++)
if
(ar[
i]>max)max=ar
[i
];
}

get
Min(i
ntar[
],int&min){
min=ar [0];
for
(i
nti=1; i
<=6;
i++)
i
f(ar[i
]<min)min=ar
[i
];
}

get
Sum(intar[
],
int&sum){
sum =0;
for
(inti
=0;i<=6;
i++)
sum +=ar[i
];
}

get
Avg(i
ntar[
],i
nt&av
g){
i
ntsum;
getSum(ar,
sum);
avg=sum /7;}

pr
int
(i
ntmax,i
ntmi
n, i
ntsum,intavg){
cout
<<"
Max="<<max<<endl;
cout
<<"
Min="
<<min<<endl;
cout
<<"
Sum="<<sum<<endl;
cout
<<"
Avg="
<<avg<<endl;
}

mai
n(){ i ntmax, min,sum, avg;
i
nta[7];read(a)
;
getMax( a,max);getMin(a,min)
;
getSum( a,sum);getAvg(a,avg)
;
pri
nt(max, min,sum, avg);
i
ntb[7];read(b)
;
getMax( b,max);getMin(b,min)
;
getSum( b,sum);getAvg(b,avg)
;
pri
nt(max, min,sum, avg);
}

Q73:
usi
ngfuncti
ons,
readani
ntegerar
rayandsear
chf
oranel
ementi
nit
:
#i
ncl
ude<i
ostr
eam.h>
26
char*noName[ ]
={"f
ir
st",
"second",
"t
hir
d",
"f
ort
h",
"f
if
th"
};
readArray
(intar[
]){
cout<<"Enter5number s:
";
for(
inti=0;i
<=4;i++)
cin>>ar[
i]
;}

r
eadVal(
int&v al
){
cout<<"Enterv
alue:
";
ci
n>>v al;
}

sear
ch(i
ntar[
],i
ntv al
,i
nt&r
){
r=-1;
for
(inti
=0;i<=4;i
++)
if
(ar
[i
]==v al
)r=i;
}

pr
int
(i
ntr){
if
(r!
=- 1)
cout<<"
Iti
s"<<noName[
r]
<<"el
ement
."
;
else
cout<<"
Notfound."
;}

mai
n(){ i ntar[
5],n,
r;
readArray(
ar);
readVal(v
al);
search(ar,
val
,r
);
print
(r)
;}

Q74:usingf uncti
on,readtwoar
ray
sandsor
tthef
ir
stoneascendi
ngandt
heot
her
onedescendi ng:
#incl
ude<i ostr
eam.h>
read(
intar[]){
cout<<"Enter5numbers:
";
for
(i
nti =0;i
<=4;i++)
ci
n>>ar[
i]
;
}

sor
tAsc(i
ntar[
]){
intt
emp;
for(
intx=0;x<=4-1;x++)
for(
inty=x+1;y <=4;y++)
if
(ar[
x]>ar [
y]){
temp=ar [x]
;
ar[x]=ar [
y];
ar[y]=t emp;}
}

sor
tDes(i
ntar
[]
){
intt
emp;
27
f
or(
intx=0;x<=4-1;x++)
for(
inty=x+1;y <=4;y++)
if
(ar[
x]<ar [
y]){
temp=ar [x]
;
ar[x]=ar [
y];
ar[y]=t emp;}
}

pr
int
(i
ntar[]
){
for
(i
nti=0;i<=4;i
++)
cout<<ar[
i]
<<""
;
cout<<endl;
}

mai
n(){inta[5]
;
read(a);
sor t
Asc(a);
print(
a);

i
ntb[ 5];
read(b);
sor t
Des(b)
;
print(
b);
}

Q75: wri
teaprogram t
oreadanumberandcal
cul
atei
fiti
spr
imeornot
:
#i nclude<iost
ream.h>
voidmai n()
{i
ntx;
char *m ;
m=" prim";
cin>>x;
for(i
nti=2;i
<x;i
++)
i
f( x%i==0)
m=" notpr i
m";
cout <<m;}

Q1:wri
teaprogram thatreadt heprimarysalar
yandt hesalesandcal
cul
atethet
otalsal
arywhi
ch
equal
sthepri
mar ysalar
ypl usthecommi ssi
ont hati
scalcul
atedas:
- Comm=2%i ft
hesal esar el
esst hanorequalt hr
eet i
mesofthepri
marysal
ary
.
- Comm=3%i ft
hesal esar emor ethanthreetimesofthepri
marysal
ary.
- Comm=5%i ft
hesal esar emor ethanfiv
et i
mesoft hepri
marysal
ary.
(comm=0.02*
bsalary,i
f(
sales<=3* bsalar
y))
(comm=0.03*bsalar
y,if
(sales>3*bsalary)
)
(comm=0.05*bsalar
y,if
(sales>5*bsalary)
)

28
#i
nclude<i ostream. h>
main()
{floatbsal ary,sales,comm;
cout<<"basi csalary=" ;
ci
n>>bsal ar y;
cout<<"sales=" ;ci
n>>sales;
i
f(sales>5* bsalary)comm=0. 05*bsal
ary;
el
se
i
f(sales>3* bsalary)comm=0. 03*bsal
ary;
el
se
i
f(sales<=3* bsalary)comm=0. 02*bsal
ary;
cout<<"netsal ary=" <<bsalar
y+comm; }

Q2: Usingf unct ions, wr i


teapr ogram t ocal culatethenumberoftypesandquanti
ti
esofastore(50
typemax. )ify ouknow t hatt het ypequant i
tyincreasedwhenwechoosei nputprocess(
in)and
enteringt henameandquant ityoft hatt y peanddecr easedi
fwechooset heprocess(out)by
enteringt henameandquant i
tyofr equi r
edoutt ype.
#include<i ostr eam. h>
#include<st r
ing. h>
structITEM {
charname[ 20] ;
i
ntqt y;
}ar [
50];
i
ntcnt =0;
read(ITEM &v al ){
cout <<"Ent eritem name: "
;
cin>>v al .
name;
cout <<"Ent erqt y :
";
cin>>v al .
qt y;}
search( ITEM v al,int&r ){
r=- 1;
for(inti=0; i
<=cnt ;i
++)
if (strcmp( ar[
i]
.name, val.name) ==0)r =i
;}
process( chart ){
ITEM v al;
read( val);
i
ntr ;
sear ch( val,r);
i
f (t
=='i'
){
if (r!=- 1)ar [
r].
qty+=v al.qty;
el se{
ar[cnt]=v al;
cnt ++; }}
i
f (t
=='o'){
if (r!=- 1)ar [
r].
qty- =val.qty;
el secout <<"I
tem notf ound! \n"; } }
print(
){
for(inti=0; i
<cnt ;i
++)
cout <<ar [i]
.name<<" \t"<<ar[i
].
qt y<<"\
n";}
mai n(){
i
nts;
do{
cout <<"( 1-in2- out3- print4-exit):"
;
cin>>s;

29
i
f(
s==1)process('
i
')
;
i
f(
s==2)process('
o'
);
i
f(
s==3)print
();
}whi
le(
s!=4);}

Q3:
apr
ogr
am t
opr
intt
hef
oll
owi
ngf
igur
eofnumber
s:

#i
ncl
ude<i
ost
ream.
h> 1

v
oidmai
n(){
intx,
i
,j
; 333

f
or(
i=1;
i
<=9;
i
+=2) 55555

{
for(
j=1;
j
<=I
;
j++) 7777777

cout
<<i
<<endl
; 999999999

f
or(
i=7;
i
>=1;
i
-=2) 7777777

{
for
(j
=1;
j
<=i
;
j(++ 55555

cout
<<i
<<endl
;} 333

#incl ude<i ostream. h>


#incl ude<coni o. h>
#incl ude<st dio.h>
void mai n()
{clrscr();
 
 //Decl ar ati
on  part
 
 int a,b,sum=0;
 
 //Pr ocessi ng par t
printf("I
nput  t
he  fi
rst 
number
\n"
);
cin>>a;
printf("I
nput  t
he  second 
number\
n")
;
cin>>b;
sum=a+b;
printf("The  Output Is\
n")
;
cout <<sum;
get ch();

//write 
a programme to 
add 
two 
i
nteger
 number
 by
 usi
ng 
funct
ion
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
i
nt add(int 
x,i
nt y)
{i
nt c=0;
c=x+y ;

30
return( c);}
void mai n()
{clrscr();
 
 //Decl arat
ion part
 
 int a,b,sum=0;
 
 //Pr ocessi ng par
t
printf("Input t
he fi
rst
 number
\n"
);
cin>>a;

pri
ntf(
"Input
 t
he second 
number
\n"
);
ci
n>>b;
sum=add( a,
b);
pri
ntf(
"The Out
put I
s\n")
;
cout<<sum;
getch()
;} 

/* wr i
te a pr ogr am in c++ t
o read 
a l
ett
er.i

the l
ett
er 
i
s smal
l
 conv
ert
 i

to 
capi
tal
and  if 
it
 is capi tal 
conv ert
 i
t t
o small
 *
/
#incl ude<i ost ream. h>
#incl ude<coni o. h>
#incl ude<st di o.h>
void  main( )
{clr
scr ();
 
 //Decl ar ation  part
 
 char  
ch,c;
//Pr ocessi ng  par t
 
   
 pr i
ntf("Input  the 
char acter
 \n")
;
 
   
 cin>>ch;
i
f((ch>=' a') &&  (ch<='z')
)
 
   
c=ch- 32;
 
  else
 
 c=ch+32;
 
  printf("
The  char acter 
after 
processi
ng I
s\n")
;
 
  cout <<c;
 
get ch( )
;} 

/wr it
e a  progr
amme  t
o add 
two 
integer
 number
 or
 sub 
or 
mul

or 
div
 accor
ding 
to 
user
 i
nput
 (
li
ke 
c
alculat or)*/
#incl ude<i ost
ream. h>
#incl ude<coni o.h>
#incl ude<st di
o.h>
void mai n()
{clrscr();
 
 //Decl arati
on part
 
 int a,b,s=0;
 
   
char  
c;
 
 //Pr ocessi ng part
printf("Input t
he fi
rst 
number\n"
);
cin>>a;
printf("Input t
he second number\
n");
cin>>b;

31
pri
ntf("I
nput t
he  operati
on 
appli
i
ed 
on t
hem\n")
;
ci
n>>c;
switch(c)

case  '
+'
:{ 
s=a+b;
 
  
  
cout <<"Resul t
 i
s "
<<s;
 
  
  
br eak;
   
  
}
 
case  '
-'
:{
 s=a-b;
 
  
  
cout <<"Resul t
 i
s "
<<s;
 
  
  
br eak;
   
  
}
 
case  '
*'
:{ 
s=a* b;
 
  
  
cout <<"Resul t
 i
s "
<<s;
 
  
  
br eak;
   
  
}
 
case  '
/'
:{
 s=a/b;
 
  
  
cout <<"Resul t
 i
s "
<<s;
 
  
  
br eak;
   
  
}
default:
cout<<" out 
of r
ange.
 so 
my 
range 
i
s + 



Onl
y";
}
getch();

/*wr ite a programme  t
o f
ind 
fact
ori
al 
of 
the 
i
nput
 i
nteger
 number
*/
#incl ude<i ostream. h>
#incl ude<coni o.h>
#incl ude<st dio.h>
void mai n()
{clrscr (
);
 
 int a,f=1, i
ndex;
 
 print f(
"Input  
the number \
n");
cin>>a;
for(index =a;i
ndex>=1; i
ndex--
)
f=index  *  
f;
printf(" the factori
al i
s\n"
);
cout <<f ;
get ch( )
;} 

/*write 
a  programme  t
o fi
nd 
power 
of 
the 
i
nput
 i
nteger
 number
sampl e:base  =2,t
o t
he power
=6,
resul
t=64
*/
#include<i ostream.h>
#include<coni o.
h>
#include<st dio.h>
void 
mai n()
{
cl
rscr ()
;
 
 i
nt  
a,b,p=1,index;

 
 printf
("I
nput the 
base number\n"
);
 
 cin>>a;
 
  pri
ntf(
"Input 
the number 
that
 become 
to 
the 
power
\n"
);
 
 cin>>b;
for(i
ndex=b;index>=1;i
ndex-
-)
p=p* a;

32
pri
ntf(
" t
he 
resul

is\
n")
;
cout<<p;
getch()
;}
 

/* 
wr i
te a 
programme to 
fi
nd 
the 
absol
ute 
val
ue 
of 
i
nteger
 number
*/
#include<i
ostream.h>
#include<conio.h>
#include<stdi
o.h>
void 
mai n()
{
cl
rscr (
);
 
 i
nt  
y;

 
 printf(
"I
nput 
the number
\n"
);
 
 cin>>y;
i
f(y>=0)
cout <<"
resul

is 
"<<y;
else
cout <<"
resul

is 
"<<-y
;
getch( )
;

/* wr i
te a progr amme  to 
fi
nd 
fact
ori
al 
of 
5 i
nput
 i
nteger
 number
*/
#incl ude<iost ream. h>
#incl ude<coni o. h>
#incl ude<st dio.h>
void mai n( )
{
clrscr (
);
 
 int a,f
=1, i
ndex;
for (i
nt i
=0; i
<5; i
++)
{printf("
Input  t
he  number\n"
);
cin>>a;
f=1;
for(index =a;index>=1; i
ndex-
-)
f=index  * 
f;
printf(" 
the fact orial
 i
s\n"
);
cout <<f<<" \
n" ;
}
get ch();

Q: wr i
teapr ogram toreadtwoar r
aysandcompar eamongeachcor
respondi
ngelement
sineach
ofthem t oproduceanewar r
ayofthelargercor
respondoft
hetwoenter
edarr
ays:
#include<i ostr
eam.h>
mai n(){inta[3][
3],
b[3]
[3]
,c[
3][
3];
i
nti ,
j;
cout <<"enterthefir
stmat r
ixelement
s"<<endl
;
for(i=0;i<3;i
++)

33
for(j
=0;j
<3;
j++)
cin>>a[
i]
[j
];
cout<<"
entert
hesecondmat
ri
xel
ement
s"<<endl
;
for(i
=0;i
<3;
i++)
for(j
=0;j
<3;
j++)
cin>>b[
i]
[j
];

for(i=0;
i<3;
i++)
for(j=0;
j<3;
j++)
{i
f(a[i]
[j
]>b[i
][
j]
)
c[i]
[j
]=a[
i][
j]
;
else
c[i]
[j
]=b[
i][
j]
;}

for(
i=0;
i
<3;
i
++)
for(
j=0;
j
<3;
j
++)
cout
<<"t
henewar
rayoft
hel
argernumber
sis"
<<c[
i]
[j
]<<endl
;
}

Q: wr i
teapr ogram toreadt wo3* 3ar r
aysandpr i
ntat hi
rdar
rayr
esul
tedfr
om mul
ti
plyi
ngt
hemain
diagonal el
ement softhet wor eadarraysandsubt r
acttheot
herel
ementsoft
hem eachel
ement
witht hecor respondingone:
#include<i ost r
eam.h>
mai n(){inta[ 3][
3],
b[3][
3],
c[3]
[3],
i,
j;
cout <<"ent er9el ement softhef i
rstarr
ay"<<endl
;
for(i
=0;i<3;i++)
for(j=0;j
<3; j
++)
cin>>a[i][
j];
cout <<"ent er9element soft hesecondar ray"
<<endl;
for(i
=0;i<3;i++)
for(j=0;j
<3; j
++)
cin>>b[i][
j];

for(i
=0;i
<3;i
++)
for(j=0;
j<3;
j++)
{i
f(i==j)
c[i]
[j
]=a[
i][
j]
*b[
j]
[j
];
else
c[i]
[j
]=a[
i][
j]
-b[
i]
[j
];
}

for(
i=0;
i
<3;i
++)
for(j
=0;
j<3;
j
++)
cout<<"
thenewar
rayi
sc[
"<<i
<<"
][
"<<j
<<"
]="
<<c[
i]
[j
]<<endl
;
}

Q: writ
eapr ogram inC++t ocal cul
atetheposi
ti
vediff
erencebet
weentwomatri
ces4X4sucht
hat
theresultoft hedifferencebetweenanyt wocorrespondingnumber
sinthear
ray
smustbe
positiv
e:
#include<iostream.h>
voidmai n(){inta[4][
4],b[
4][
4],
c[4][
4],
i,
j;
cout<<"entert heelement softhef i
rstmatr
ic"
<<endl
;
for(i
=0;i<4;i
++)
for(j
=0;j<4;j
++)
cin>>a[i
][j
];

34
cout<<"ent ertheelementsofthesecondmat
ri
c"<<endl;
for(i=0;
i<4;i++)
for(j=0;
j<4;j++)
cin>>b[i
][j]
;
for( i
=0;i<4;i++)
for(j=0;
j<4;j++)
{i
f(a[i]
[j
]>=b[ i]
[j
])
c[i]
[j
]=a[
i][j
]-b[i
][
j]
;
else
c[i]
[j
]=b[
i][j
]-a[i
][
j]
;}
for(i=0;
i<4;i++)
for(j=0;
j<4;j++)
cout<<"theposi t
ivedi
ff
erenceoft
het womat
ri
cesis"<<c[
i]
[j
]<<endl
;
}

Q: writ
eapr ogram tocal
culatethesquar erootofthenumber
ssuchas( 4,
9,
16,
25,
36)t
hathas
i
ntegerr ootswi thoutusi
ngt he(sqrt)funct
ion:
#include<iostream.h>
voidmai n(){intn,i
;
cout<<"enteranumbert ocalculateitssquareroot
,zer
ototer
minat
e"<<endl
;
ci
n>>n;
while(n!
=0)
{f
or( i
=1;i<=n;i
++)
{i
f(i*i
==n)
cout<<"thesquar erootof"<<n<<"="<<i
<<endl;
}
ci
n>>n; }
}

Q: writ
eapr ogram i
nc++t oconvert2Darrayto1Darrayandf
ondt
hemaxi
mum numberi
nthe
newar ray :
#include<i ostream.h>
voidmai n(){i
nta[2][
3],
b[6]
,i
,j
,
k=0,max;
cout<<"ent ert hematri
xelements"<<endl
;
for(i
=0;i<2;i++)
for(j
=0;j<3;j++)
cin>>a[i][
j]
;
for(i
=0;i<2;i++)
for(j
=0;j<3;j++)
cout<<a[ i]
[j
]<<"";
cout<<"\ n";
for(i
=0;i<2;i++)
for(j
=0;j<3;j++)
{b[k]
=a[i][j
];
k=k+1; }
for(i
=0;i<k;i++)
cout<<b[ i]
<<"" ;
cout<<"\ n";
max=b[ 0] ;
for(i
=0;i<k;i++)
{if(b[i
]>max)
max=b[ i];}
cout<<"themaxi mum numberinthenewmat r
ixi
s"<<max;
cin>>i;
}

35
Q: writ
eapr ogr am t
oout
putt
hef
oll
owi
ngf
igur
e:
1
333
55555
7777777
999999999
7777777
55555
333
1
#include<iostr eam.h>
voidmai n( ){intx,i
,
j;
for(i
=1;i<=9;i+=2)
{for(j
=1;j<=i;
j++)
cout<<i;
cout<<endl ;
}
for(i
=7;i>=1;i-=2)
{for(
j=1;
j<=i;j
++)
cout<<i;
cout<<endl ;
}
cin>>x;}

Q: writ
eapr ogram i
nC++t
oshowt
hef
oll
owi
ngf
igur
e:
1
333
55555
7777777
999999999
7777777
55555
333
1
#include<iostream.h>
voidmai n(
){inti,
j
,k;
for(i
=1;i<=9;
i+=2)
{for(
k=i;k<=9;k+=2)
cout<<"" ;
for(j
=i;
j>0;j
--
)
cout<<i;
cout<<"\ n"
;}
for(i
=7;i>=1;
i-=2)
{for(
k=i;k<=9;k+=2)
cout<<"" ;
for(j
=i;
j>0;j
--
)
cout<<i;
cout<<"\ n"
;}
cin>>i;
}

Q:wr
it
eaprogr
am topr
intt
hef
oll
owi
ngnumber
susi
ngf
orl
oop:
110100100010000
220200200020000
330300300030000
440400400040000

36
550500500050000
#include<iostream.h>
#include<mat h.h>
voidmai n(){inti,
j
,k;
for(i
=1;i
<=5;i++)
{for(
j=0;
j<=4;j++)
cout<<i*pow( 10,j
)<<""
;
cout<<"\n";
}
cin>>k;}

Q: writ
eapr ogram todispl
ayt
hemul
ti
plyt
abl
eof(
1-10)number
s:
#include<iostream.h>
voidmai n(){
for(i
ntx=1;x<11;x++)
{for(i
nty =1;y
<11;y++)
cout <<y<<"*"
<<x<<"="
<<x*y
<<"
\t"
;
cout <<endl;}}

Q: writeac++pr ogram tosum twoarraysandpr i


ntther
esul
tar
rayi
na2Dmat
ri
xfor
m:
#include<i ost ream.h>
voidmai n() {inta[3][
3],
b[3]
[3]
,c[
3][
3],
i
,j
;
cout<<" ent er9el ement softhefir
starr
ay"<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j
];
cout<<" ent er9el ement softhesecondar r
ay"<<endl
;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j
];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i]
[j
]=a[i][
j]+b[ i
][
j]
;
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
cout<<" c["<<i<<"]["
<<j<<"
]="
<<c[i
][
j]
<<"" ;
cout<<endl ;}}

37

You might also like