C++ Programming Exercises for Beginners
C++ Programming Exercises for Beginners
Ch# 1 (Programs)
1) Write a program enter two 2) Write a program enter two 3) Write a program enter age in
numbers from user and display sum numbers from user and display years and convert int o month
of numbers. sum, multiply, subtraction & and days.
# include <iostream> division of the numbers. # # include <iostream>
using namespace std; include <iostream> using namespace std;
int main () using namespace std; int main ()
{ int main () {
int n1, n2, s; { int y, m, d;
cout<< ”Enter 1st number=”; int n1, n2, s, sub, m, d; cout<<”Enter age in years=”;
cin>>n1; cout<<”Enter 1st number=”; cin>>y;
cout<< ”Enter 2nd number=”; cin>>n1; m=y*12;
cin>>n2; cout<<”Enter 2nd number=”; d=m*30;
s=n1+n2; cin>>n2; cout<<”Month=”<<m;
cout<<”Sum=”<<s; s=n1+n2; cout<<”Days=”<<d;
return 0; sub=n1-n2; return 0;
} m=n1*n2; }
d=n1/n2;
cout<<”Sum=”<<s;
cout<<”Substraction=”<<sub;
cout<<”Multiplication=”<<m;
cout<<”Division=”<<d;
return 0;
}
4) Write a program enter distance 5) Write a program enter a 6) Write a program enter
in km and convert it int o meters number to display the square and diameter of circle and print out
and cm. cube of this number. the radius of the circle. #
# include <iostream> # include <iostream> include <iostream>
using namespace std; using namespace std; using namespace std;
int main () int main () int main ()
{ { {
int km, m ,cm; int n, sq, cb; float d, r;
cout<<”Enter distance in kilometer=”; cout<<”Enter a number=”; cout<<”Enter diameter=”;
cin>>km; cin>>n; cin>>d;
m=km*1000; sq=n*n; r=d/2;
cm=m*100; cout<<”Square=”<<sq; cout<<”Radius=”<<r;
cout<<”Meter=”<<m; cb= n*n*n; return 0;
cout<<”Centimeter=”<<cm; cout<<”Cube=”<<cb; }
return 0; return 0;
} }
7) Write a program enter radius of 8) Write a program enter radius 9) Write a program enter
the circle print out the area and of sphere print out the area and temperature in Fahrenheit
circumference. circumference of sphere. # and convert int o Celsius.
# include <iostream> include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main () int main () int main ()
{ { {
float r, a, c; float r, a, c; float f, c;
cout<<”Enter radius of the circle=”; cout<<”Enter radius of the sphere=”; cout<<”Enter temp. fahrenheit =”;
cin>>r; cin>>r; cin>>f;
a=3.14*r*r; a=4*3.14*r*r; c=5.0/9.0*(f-32);
c=2*3.14*r; c=4/3*3.14*r*r*r; cout<<”Celsius=”<<c;
cout<<”Area=”<<a; cout<<”Area=”<<a; return 0;
cout<<”Circumference=”<<c; cout<<”Circumference =”<<c; }
return 0; return 0;
} }
10) Write a program enter temp. in 11) Write a program enter total 12) Write a program enter sale
celsius and convert int o Fahrenheit. number of items and cost per price and profit from user, print
# include <iostream> item, print out the total bill. # out total purchase price. #
using namespace std; include <iostream> include <iostream>
int main () using namespace std; using namespace std;
{ int main () int main ()
float f, c; { {
cout<<”Enter temperature in celsius =”; int n, cpi, bill; int s, pft, pur;
cin>>c; cout<<”Enter total no. of item & cout<<”Enter sale price & profit =”;
c=9.0/5.0*(c+32); cost per item”; cin>>s>>pft;
cout<<” Fahrenheit =”<< f; cin>>n>>cpi; pur= s- pft;
return 0; bill=n*cpi; cout<<”Purchase Price =”<<pur;
} cout<<“Total Bill”<<bill; return 0;
return 0; }
}
13) Write a program enter purchase 14) Write a program enter two 15) Write a program enter two
price and profit from user, print out numbers from user and swap numbers from user and swap
the sale price. the values of both number. # the values of both variables
# include <iostream> include <iostream> without taking third variable. #
using namespace std; using namespace std; include <iostream>
int main () int main () using namespace std;
{ { int main ()
int pur, pft, s,; int a, b, temp; {
cout<<”Enter pur. price & profit =”; cout<<”Enter two number=”; float a, b;
cin>>pur>>pft; cin>>a>>b; cout<<”Enter two number=”;
pur= pur + pft; temp = a; cin>>a>>b;
cout<<” Sale Price =”<< s; a = b; a=a+b;
return 0; b = temp; b=a-b;
} cout<<”First num. =”<<a; a=a-b;
cout<<”Second num=”<<b; cout<<”After Swaping values of both
return 0; numbers=”<<a<<b;
} return 0;
}
16) Write a program enter four digit 17) Write a program enter four 18) Write a program enter marks
number print out the sum of digits. # digit number print out the of 4-subjects and total numbers
include <iostream> reverse of that number. # print out the percentage and
using namespace std; include <iostream> average, assume all subjects are
int main () using namespace std; 100 number.
{ int main () # include <iostream>
int n,d4,d3,d2,d1, s; { using namespace std;
cout<<”Enter four digit number=”; int n,d4,d3,d2,d1, rev; int main ()
cin>>n; cout<<”Enter four digit number=”; {
d4=n%10; cin>>n; int urd, eng, isl, comp, tn;
n=n/10; d4=n%10; float per, avg;
d3=n%10; n=n/10; cout<<”Enter urd., eng., isl., comp.
n=n/10; d3=n%10; and total numbers=”;
d2=n%10; n=n/10; cin>>urd>>eng>>isl>>comp>>tn;
n=n/10; d2=n%10; avg=(urd+eng+isl+comp)/4.0;
d1=n; n=n/10; per=(urd+eng+isl+comp)/tm*100;
s=d1+d2+d3+d4; d1=n; cout<<”Average=”<< avg;
cout<<”Sum of four digits=”<<s; rev=d4*1000+d3*100+d2*10+d1; cout<<”Percentage=”<< per;
return 0; cout<<”Reverse=”<<rev; return 0;
} return 0; }
}
19) Write a program enter five digit 20) Write a program enter 21) Write a program enter price of
number print out the sum of 1st, 3rd, price of any item and give 10% any item and give 10% tax then
& 5th digit. discount then display total display total bill.
# include <iostream> bill. # include <iostream>
using namespace std; # include <iostream> using namespace std;
int main () using namespace std; int main ()
{ int main () {
int n,d1,d2,d3,d4,d5, s; { int p, t, tb;
cout<<”Enter five digit number=”; int p, d, tb; cout<<”enter price of item =”;
cin>>n; cout<<”Enter price of item =”; cin>>p;
d5=n%10; cin>>p; t=p/0.10;
n=n/10; d=p*10/100; tb=p+t;
d4=n%10; tb=p-d; cout<<“Total Bill”<<tb;
n=n/10; cout<<“total bill”<<tb; return 0;
d3=n%10; return 0; }
n=n/10; }
d2=n%10;
n=n/10;
d1=n;
s=d1+d3+d5;
cout<<“ Sum of Digit”<< s;
return 0;
}
22) Write a program enter basic 23) Write a program enter 24) Write a program enter your
salary from user than give 10% amount for ATM machine and age in days and then convert int o
house rent, 20% medical allowance, then print total number of years, months, weeks and
25% bonus and print gross salary. # notes five thousand, one remaining days.
include <iostream> thousand, five hundred & one # include <iostream>
using namespace std; hundred. # include <iostream> using namespace std;
int main () using namespace std; int main ()
{ int main () {
Float bs, hr, ma, b, gs; { Int d,y,m,w;
cout<<”Enter basic salary=”; int amt,n5000, n1000, n500, cout<<”Enter age in Days=”;
cin>>bs; n100; cin>>d;
hr= bs*10/100; cout<<”Enter amount for ATM=”; y=d/365;
ma= bs *20/100; cin>>amt; d=d%365;
b= bs *25/100; n5000=amt/5000; m=d/30;
gs= bs+hr+ma+b; amt=amt%5000;
d=d%30;
cout<<”Gross Salary=”<< gs; n1000=amt/1000;
w=d/7;
return 0; amt=amt%1000;
} n500=amt/500; d=d%7;
amt=amt%500; cout<<”Years =”<<y;
n100=amt/100; cout<<”Month =”<<m;
cout<<” 5000=”<<5000; cout<<”Weak =”<<w;
cout<<” 1000=”<<1000; return 0;
cout<<” 500=”<<500; }
cout<<” 100=”<<100;
return 0;
}
25) Write a program enter time in 26) Write a program enter 27) Write a program enter a
seconds and print out hours, divider and divisor from user, floating/real number, print out the
minutes and remaining seconds. # print out remainder without round off int eger number. #
include <iostream> using remainder operator. # include <iostream>
using namespace std; include <iostream> using namespace std;
int main () using namespace std; int main ()
{ int main () {
int s, h, m, rs; { int i;
cout<<”Enter time in seconds=”; int a,b,c,d,rem; float f;
cin>>s; cout<<”Enter divider=”; cout<<”Enter floating number=”;
h=s/3600; cin>>a; cin>>f;
s=s%3600; cout<<”Enter divisor=”; i=f+0.5;
m=s/60; cin>>b; cout<<”integer after round
s=s%60;
c=a/b; off=”>>i;
rs=s/60;
d=b*c; return 0;
cout<<”Hours =”<<h;
cout<<”Minutes =”<<m; rem=a-d; }
cout<<”remaning seconds=”<<rs; cout<<”Reminder=”>>rem;
return 0; return 0;
} }
28) Write a program enter 29) Write a program enter 30) Write a program enter
character from user and convert int ASCII code from user and character from user and print out
o ASCII code. convert int o character. # next character.
# include <iostream> include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main () int main () int main ()
{ { {
Int ASCII; Int ASCII; Int ch;
char ch; char ch; cout<<”Enter character=”;
cout<<”Enter character=”; cout<<”Enter ASCII code=”; cin>>ch;
cin>>ch; cin>> ASCII; ch=ch+1;
ASCII=(int) ch; ch=(char) ASCII; cout<<”Next Character=”<<ch;
cout<<”ASCII Code=”<<ASCII; cout<<” Character=”<<ch; return 0;
return 0; return 0; }
} }
Ch# 2 (Programs)
1) Write a program enter marks 2) Write a program enter two 3) Write a program enter a
from user if marks are greater than numbers from user print both number from user print is it
or equal to 40 then is pass otherwise numbers are equal or not. even or odd.
fail. # include <iostream> # include <iostream>
# include <iostream> using namespace std; using namespace std;
using namespace std; int main () int main ()
int main () { {
{ int x, y; int n;
int m; cout<<”Enter two Numbers=”; cout<<”Enter a Number =”;
cout<<”Enter Marks=”; cin>>x>>y; cin>>n;
cin>>m; if(x==y) if(n%2==0)
if(m >= 40) cout<<”Both numbers are equal”; cout<<”Even”;
cout<<”Pass”; else else
else cout<<”Both numbers are not equal”; cout<<”Odd”;
cout<<”Fail”; return 0; return 0;
return 0; } }
}
4) Write a program enter a number 5) Write a program enter two 6) Write a program enter two
from user print weather is it numbers print is it 1st number is numbers print is it 1st number
divisible b 3 or not. multiple of 2nd number or not. is square of 2nd number or not.
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main () int main () int main ()
{ { {
int h; int n1, n2; int x, y;
cout<<”Enter a Number =”; cout<<”Enternter two Numbers=”; cout<<”Enter two Numbers=”;
cin>>h; cin>>n1>>n2; cin>>x>>y;
if (h%3==0) if (n1%n2==0) if (x==y*y)
cout<<”Divisible by 3”; cout<<”1st No. is multiple of 2nd num.”; cout<<”1st no is square of 2nd no.”;
else else else
cout<<”Not divisible by 3”; cout<<”Not multiple”; cout<<”Not square”;
return 0; return 0; return 0;
} } }
7) Write a program enter years from 8) Write a program enter 4-digit 9) Write a program enter a
user print is it leap year or not. number from user print weather its number from user print the
# include <iostream> number is palindrome or not. absolute number.
using namespace std; # include <iostream> # include <iostream>
int main () using namespace std; using namespace std;
{ int main () int main ()
int y; { {
cout<<”Enter Year =”; int n, rev, org, d4, d3, d2, d1; int n, abs;
cin>>y; cout<<”Enter 4-digits Number =”; cout<<”Enter a number =”;
if (y%4==0) cin>>n; cin>>n;
cout<<”It is a leap year”; org=n; if(n<0)
else d4=n%10; abs=n*(-1);
cout<<” It is not leap year”; n=n%10; else
return 0; d3=n%10; abs=n;
} n=n%10; cout<<” Absolute Number=”<abs;
d2=n%10; return 0;
MUHAMMAD SHERAZ(MSCS) (03023331948) 7
NATIONAL COLLEGE HASILPUR C++
n=n%10; }
d1=n;
rev=d4*1000+d3*100+d2*10+d1;
if(org==rev)
cout<<”Palindrome”;
return 0;
}
10) Write a program enter a number 11) Write a program enter 12) Write a program enter a
from user print this number is +ve, purchase price and sale price from character from user, print is it
-ve or zero. user, print it is profit or loss or not capital, small, special symbol or
# include <iostream> profit or not loss. digit.
using namespace std; # include <iostream> # include <iostream>
int main () using namespace std; using namespace std;
{ int main () int main ()
int n; { {
cout<<”Enter a Number =”; int pur, sal; char ch;
cin>>n; cout<<”Enter Purchase Price and Sale cout<<”Enter a Character=”;
if(n>0) Price =”; cin>>ch;
cout<<”+ve”; cin>>pur>>sal; if(ch>=’A’ && ch<=’Z’)
else if(n<0) if(sal>pur) cout<<”Capital Letter”;
cout<<”-ve”; cout<<”Profit”; else if(ch>=’a’ && ch<=’z’)
else else if(pur>sal) cout<<”Small Letter”;
cout<<”Zero”; cout<<”Loss”; else if(ch>=’0’ && ch<=’9’)
return 0; else cout<<”Digit”;
} cout<<”Not Profit or Not Loss”; else
return 0; cout<<”Special Symbol”;
} return 0;
}
13) Write a program enter a 14) Write a program enter the age 15) Write a program enter
character from user, print capital of Aslam, Akram & Ahmad, print three numbers from user, print
letter if it small and print small letter which one is youngest. out the maximum number.
if it is capital. # include <iostream> # include <iostream>
# include <iostream> using namespace std; using namespace std;
using namespace std; int main () int main ()
int main () { {
{ int As, Ak, Ah; int a, b, c;
char ch; cout<<”Enter age of Aslam, Akram and cout<<”Enter Three Numbers =”;
cout<<”Enter a Character=”; Ahmad =”; cin>>a>>b>>c;
cin>>ch; cin>>As>>Ak>>Ah; if (a>b && a>c)
if(ch>=’A’ && ch<=’Z’) if (As<Ak && As<Ah) cout<<”a is Maximum”;
ch=ch+32; cout<<”Aslam is Youngest”; else if (b>a && b>c)
else else if (Ak<As && Ak<Ah) cout<<”b is Maximum”;
ch=ch-32; cout<<”Akram is Youngest”; else
cout<<”Letter”<<ch; else cout<<”c is Maximum”;
return 0; cout<<”Ahmad is Youngest”; return 0;
} return 0; }
}
16) Write a program enter a 17) Write a program enter age from else
character from user, print it is vowel user, print is it child or teen age or cout<<”Old=”;
or not. young or old. According to following return 0;
# include <iostream> (1-10=child), (11-18=teen age), }
using namespace std; (19-35=young), (35+=old).
int main () # include <iostream>
{ using namespace std;
char ch; int main ()
cout<<”Enter a Character=”; {
cin>>ch; int age;
if(ch==’A’||ch==’a’||ch==’E’||ch==’e’|| cout<<”Enter Age=”;
ch==’I’||ch==’i’||ch==’O’||ch==’o’||ch==’ cin>>age;
U’|| ch==’u’) if(age>1 && age<=10)
cout<<”Vowel=”; cout<<”Child”;
else else if(age>11 && age<=18)
cout<<”Not Vowel”; cout<<”Teen Age”;
return 0; else if(age>19 && age<=15)
} cout<<”Young”;
18) Write a program enter marks 19) Write a program enter x-axis & 20) Write a program enter
from user, print out grade according y-axis from user print whether the three angles of triangles, print
to following: (90+=A+) (80-89=A) point is at x-axis & y-axis or origin whether is it right angle
(70-79=B+) (60-69=B) (50-59=C) or both. triangle or equilateral triangle,
(less than 50=F). # include <iostream> isosceles or scalene triangle.
# include <iostream> using namespace std; # include <iostream>
using namespace std; int main () using namespace std;
int main () { int main ()
{ int x, y; {
int m; cout<<”Enter x-axis and y-axis=”; int s1, s2, s3;
cout<<”Enter Marks=”; cin>>x>>y; cout<<”Enter three angles of
cin>>m; if(x !=0 && y==0) triangle=”;
if(m>=90) cout<<”Point is at x-axis”; cin>>s1>>s2>>s3;
cout<<”A+”; else if(x ==0 && y!=0) if(s1==90||s2==90||s3==90)
else if(m>=80 && m<=89) cout<<”Point is at y-axis”; cout<<”Right Angle Triangle”;
cout<<”A”; else if(x ==0 && y==0) else if (s1==s2 && s2==s3 &&
else if(m>=70 && m<=79) cout<<”Origin”; s3==s1)
cout<<”B+”; else cout<<” Equilateral Triangle”;
else if(m>=60 && m<=69) cout<<”Both at x-axis and y-axis”; else if (s1==s2||s2==s3||s3==s1)
cout<<”B”; return 0; cout<<”Isosceles”;
else if(m>=50 && m<=59) } else
cout<<”C”; cout<<”Scalene”;
else return 0;
cout<<”F”; }
return 0;
}
21) Write a program enter total 22) Write a program for library 23) Write a program enter total
items and cost per item and calculate enter late days for returning book units of electricity print total
bill if bill is greater than one and print total fine by following: bill by following:
thousand than give 20% discount, (1—10=5 rs per day) (11—20=10 rs (1—100=50 pu) (101—
otherwise 10% discount print out per day) (21-26=15 rs per day) 300=70pu) (301--500=100 pu)
the total bill. (26—30=20 rs per day) (30+=your (500+=120 pu)
# include <iostream> allotment is canceled) # include <iostream>
using namespace std; # include <iostream> using namespace std;
int main () using namespace std; int main ()
{ int main () {
int ti, cpi, bil, dis, tb; { int tu;
cout<<”Enter total item and cost per int d; cout<<”Enter total unit of
item=”; cout<<”Enter late days for returning Electricity=”;
cin>>ti>>cpi; book=”; cin>>tu;
if(bill>1000) cin>>d; if(tu>=1 && tu<=100)
dis=bill*20/100; if(d>=1 && d<=10) cout<<”Bill<<”tu*50;
else cout<<”Fine<<”d*5; else if(tu>=101 && tu<=300)
dis=bill*10/100; else if(d>=11 && d<=20) cout<<”Bill<<”tu*70;
tb=bill-dis; cout<<”Fine<<”d*10; else if(tu>=301 && tu<=500)
cout<<”Total Bill”<<tb; else if(d>=21 && d<=25) cout<<”Bill<<”tu*100;
return 0; cout<<”Fine<<”d*15; else
} else if(d>=26 && d<=30) cout<<”Bill<<”tu*120;
cout<<”Fine<<”d*20; return 0;
else }
cout<<”Your Allotment is Canceled”;
return 0;
}
24) Write a program enter two 25) Write a program enter a day 26) Write a program enter
numbers and one operator from user number from user and print out temperature from user, if the
perform crossponding operation crossponding day according to no. user enter (f) than convert int o
according to operator if user enter If user enter (1) than display fahrenheit and if user enter (c)
‘+’ symbol then perform addition and Monday………….. etc. than convert int o Celsius.
so on. # include <iostream> # include <iostream>
# include <iostream> using namespace std; using namespace std;
using namespace std; int main () int main ()
int main () { {
{ int d; float t;
int a, b; cout<<”Enter day number 1-7=”; char ch;
char ope; cin>>d; cout<<”Enter Temperature=”;
cout<<”Enter two numbers and one if(d==1) cin>>t;
operator=”; cout<<”Monday”; cout<<”Enter F for fahrenheit and
cin>>a>>b>>ope; else if(d==2) C for celsius=”;
if(ope==’+’) cout<<”Tuesday”; cin>>ch;
cout<<”Addition” <<a+b; else if(d==3) if(ch==’F’)
else if(ope==’-’) cout<<”Wednesday”; cout<<”Fahrenheit”<<
cout<<”Subtraction” <<a-b; else if(d==4) 9.0/5.0*(t+32);
else if(ope==’*’) cout<<”Thursday”; else if(ch==’C’)
cout<<”Multiplication” <<a*b; else if(d==5) cout<<”Celsius”<<
else if(ope==’/’) cout<<”Friday”; 5.0/9.0*(t-32);
cout<<”Division” <<a/b; else if(d==6) else
else if(ope==’%’) cout<<”Saturday”; cout<<”Invalid Choice”;
cout<<”Remainder” <<a%b; else if(d==7) return 0;
else cout<<”Sunday”; }
cout<<”Invalid Operator”; else
MUHAMMAD SHERAZ(MSCS) (03023331948) 10
NATIONAL COLLEGE HASILPUR C++
Ch# 3 (Programs)
MUHAMMAD SHERAZ(MSCS) (03023331948) 12
NATIONAL COLLEGE HASILPUR C++
1) Write a program display 2) Write a program print following 3) Write a program print
“Pakistan” ten times on screen by series by using while loop. following series by using while
using While. (0,2,4,6, 8, …..500) loop. (1 2 3 4 ……….10)
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int i=1; int i=0; int i=0;
while(i<=10) while(i<=500) while(i<=10)
{ { {
cout<<”Pakistan”; cout<<i<<endl; cout<<i<<”/t”;
i++; i=i+2; i=i+1;
} } }
return0; return0; return0;
} } }
4) Write a program print following 5) Write a program print following 6) Write a program print
series by using while loop. 5/6 6/7 series by using while loop. 10, 100, following series by using while
7/8 8/9………….100/101 1000, 10000, 100000 loop. 2, 4, 16, 256, ………..
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int i=5; int i=10; int i=2;
while(i<=100) while(i<=100000) while(i<=256)
{ { {
cout<<i/(i+1)<<”/t”; cout<<i<<”/t”; cout<<i<<”/t”;
i=i+1; i=i*10; i=i*i;
} } }
return0; return0; return0;
} } }
7) Write a program print following 8) Write a program print sum from 9) Write a program print the
series by using while loop. 5, 10, 1 to 10 (while) multiplying from following
20, 40, 80, 160, 320 ……….. (1+2+3+4+5+6+7+8+9+10). series. 8*10*15*20 …….*50
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int i=5; int i=1, s=0; int i=5, m=1;
while(i<=320) while(i<=101) while(i<=50)
{ { {
cout<<i<<”/t”; s=s+i; m=m*i;
i=i*2; i=i+1; i=i+5;
} } }
return0; cout<<”sum=”<<s; cout<<”multiplying”<<m;
} return0; return0;
} }
10) Write a program print sum from 11) Write a program enter a number 12) Write a program enter
by while (1+2+3+4+5+…………+n). print out the factorial of that number power & base from user print
# include <iostream> n!=nx,………3*2*1. out total result.
using namespace std; # include <iostream> # include <iostream>
int main() using namespace std; using namespace std;
{ int main() int main()
int i=1, s=0,n; { {
cout<<”enter last number=”; int f=1,i,n; int p,b,i=1,r=1;
cin>>n; cout<<”enter a number=”; cout<<”enter power & base=”;
while(i<=n) cin>>n; cin>>p>>b;
{ i=n; while(i<=p)
s=s+i; while(i>=1) {
i=i+1; { r=r*b;
} f=f*i; i=i+1;
cout<<”sum=”<<s; i=i-1; }
return0; } cout<<”result=”<<r;
} cout<<”factorial=”<<f; return0;
return0; }
}
13) Write a program enter a table no 14) Write a program enter a number 15) Write a program enter a
from user print table from 1 to 10. print out the reverse of that number. number print weather that
# include <iostream> # include <iostream> number is palindrome or not.
using namespace std; using namespace std; # include <iostream>
int main() int main() using namespace std;
{ { int main()
Int t, i=1; Int n,d,rev=0; {
cout<<”enter table no=”; cout<<”enter a number=”; Int n,d,rev=0,org;
cin>>t; cin>>n; cout<<”enter a number=”;
while(i<=10) while(n !=0) cin>>n;
{ { org=n;
Cout<<t<<”*”<<i<<”=”t*i<<endl; d=n%10; while(n !=0)
I++; n=n/10; {
} rev=rev*10+d; d=n%10;
return0; } n=n/10;
} cout<<”reverse=”<<rev; rev=rev*10+d;
return0; }
} If(org==rev)
cout<<”palindrome=”;
else
cout<<”not palindrome=”;
return0;
}
(Do-While) arm=arm+d*d*d;
16) Write a program enter a number }
print weather that number is While(n !=0)
Armstrong or not. If(arm==org)
# include <iostream> cout<<”arm strong=”;
using namespace std; else
int main() cout<<”not arm strong =”;
{ return0;
Int n, d, arm=0,org; }
cout<<”enter a number=”;
cin>>n;
org=n;
do
{
d=n%10;
n=n/10;
(For-loop) 18) Write a program enter power & 19) Write a program print all
17) Write a program enter a number base from user print out total result. even numbers b/w (1,2,3…100)
print out the factorial of number. # include <iostream> # include <iostream>
# include <iostream> using namespace std; using namespace std;
using namespace std; int main() int main()
int main() { {
{ Int p, b, i, r=1; Int i;
Int n, i, f=1; cout<<”enter power & base=”; for(i=1; i<=100; i++)
cout<<”enter any number=”; cin>>b>>p; {
cin>>n; for(i=1; i<=p; i++) If(i%2==0)
for(i=n; i>=1; i++) r=r*b; cout<<i<<”/t”;
f=f*I; cout<<”result=”<<r; }
cout<<”factorial=”<<f; return0; return 0;
return 0; } }
}
20) Write a program enter (100) 21) Write a program enter (50) fifty (V. Imp) 22) Write a program
hundred numbers from user print numbers from user print how much enter a no print weather is it
how much +ve, -ve or zero are enter. even or odd. prime or composite.
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int i, p=0, neg=0, z=0, n; int i, n, even, odd; int i, n;
for(i=1; i<=100; i++) for(i=1; i<=n; i++) cout<<”enter a number=”;
{ { cin>>n;
cout<<”enter a number=”; cout<<”enter total numbers=”; for(i=2; i<=n; i++)
cin>>n; cin>>n; {
if(n>0) if(n%2==0) If(n%i==0)
p=p+1; even=even+1; break;
else if (n<0) else }
neg=neg+1; odd=odd+1; If(n==i)
else } cout<<”prime number=”;
z=z+1; cout<<”even number=”<<even; else
} cout<<”odd number=”<<odd; cout<<”composite number=”;
cout<<”total +ve=”<<p; return 0; return 0;
cout<<”total -ve=”<<neg; } }
cout<<”total zero=”<<z;
return 0;
}
23) Write a program enter a no print 24) Write a program enter a no from 25) Write a program enter a
weather is it perfect sequare or not. user print out prime factor. (while) decimal no, print no or binary.
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int i, n; int i, n; int n, i=1, d, bin=0;
cout<<”enter a number=”; cout<<”enter a number=”; cout<<”enter a number=”;
cin>>n; cin>>n; cin>>n;
for(i=2; i<=n; i++) while(n !=1) while(n !=1)
{ { {
If(i*i==n) If(n%i==0) d=n%2;
break; { n=n/2;
} n=n/i; bin=bin+d*i;
If(i*i==n) cout<<i<<”/t”; i=i*10;
cout<<”perfect square=”; } }
else else return 0; cout<<”binary=”<<bin;
cout<<”not perfect square=”; i++ } return 0;
return 0; } }
}
26) Write a program print out the (Nested loop) (Nested loop)
fibonic series. 0 1 1 2 3 5 8….n 27) Write a program print out the 28) Write a program print out
# include <iostream> following. the following.
using namespace std; # include <iostream> # include <iostream>
int main() using namespace std; using namespace std;
{ int main() int main()
int n, a=, b=1, c=0; { {
cout<<”enter last number=”; int i, j; int i, j;
cin>>n; for (i=1; i<=4; i++) for (i=1; i<=4; i++)
while(c<=n) { {
{ for (j=1; j<=i; j++) for (j=1; j<=i; j++)
Cout<<c<<endl; cout<<”*”; cout<<”j”;
a=b cout<<endl; cout<<endl;
b=c } }
c=a+b return 0; return 0;
} } }
return 0;
}
29) Write a program enter 1 to 100
prime factors.
# include <iostream>
using namespace std;
int main()
{
int i, j;
for (j=1; j<=100; j++)
{
for (i=2; i<=j; i++)
{
if(j%i==0)
break;
}
if(i==j)
cout<<”i”<<endl;
}
return 0;
}
Ch# 4 (Array)
1) Write a program enter ten values 2) Write a program enter 50 values 3) Write a program enter 20
in an array displap all values on the in an array displap all values on the values in an array displap all
screen. screen. even values on the screen.
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int Ar[10] i; int Ar[50] i; int Ar[20] i;
for(i=0; i<=9; i++) for(i=0; i<=49; i++) for(i=0; i<=19; i++)
{ { {
cout<<”enter number”; cout<<”enter number”; cout<<”enter number”;
cin>>Ar[i]; cin>>Ar[i]; cin>>Ar[i];
} } {
for(i=0; i<=9; i++) for(i=0; i<=49; i++) for(i=0; i<=19; i++)
cout<<Ar[i]<<endl; cout<<Ar[i]<<endl; }
return0; return0; If(Ar[i]%2==0)
} } cout<<Ar[i]<<endl;
}
return0;
}
4) Write a program enter 75 values 5) Write a program enter 100 6) Write a program enter 100
from user in an array count how values in an array count how much values print out sum of all even
much even and odd. +ve, -ve or zero values. and odd values.
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int Ar[75], i, E=0, O=0; int Ar[100], i, P=0, N=0, Z=0; int Ar[100], i, E=0, O=0;
for(i=0; i<=74; i++) for(i=0; i<=99; i++) for(i=0; i<=99; i++)
{ { {
cout<<”enter number”; cout<<”enter number”; cout<<”enter number”;
cin>>Ar[i]; cin>>Ar[i]; cin>>Ar[i];
} } }
for(i=0; i<=74; i++) for(i=0; i<=99; i++) for(i=0; i<=99; i++)
{ { If(Ar[i]%2==0)
If(Ar[i]%2==0) If(Ar[i]%>0) E=E+A[i];
E=E+1; P=P+1; else
else else if(Ar[i]<0) 0=0=Ar[i]
O=O+1; N++; }
} else cout<<”sum of even=”<<E;
cout<<”enter even=”<<E; Z=Z+1; cout<<”sum of odd=”<<O;
cout<<”enter odd=”<<O; } return0;
return0; cout<<”enter positive=”<<p; }
} cout<<”enter negative=”<<n;
cout<<”enter zero=”<<z;
return0;
}
7) Write a program enter 20 values 8) Write a program enter 10 values 9) Write a program enter 10, 10
print out sum of all values. from the array, display all values in in two arrays and sum of both
# include <iostream> reverse order. arrays display in third array,
using namespace std; # include <iostream> print out third array on screen.
int main() using namespace std; # include <iostream>
{ int main() using namespace std;
int Ar[20], i, sum=0; { int main()
for(i=0; i<=19; i++) int Ar[10], i; {
{ for(i=0; i<=9; i++) int A[10], B[10], C[10], i;
cout<<”enter number”; { for(i=0; i<=9; i++)
cin>>Ar[i]; cout<<”enter values”; {
sum=sum=Ar[i]; cin>>Ar[i]; cout<<”enter values in 1st array”;
} } cin>>A[i];
cout<<”sum of all numbers=”<<sum; for(i=9; i>=0; i--) cout<<”enter values in 2nd array”;
return0; cout<<Ar[i]<<endl; cin>>B[i];
} return0; C[i]=A[i] + B[i]
} }
for(i=0; i<=9; i++)
cout<<c[i]<<endl;
return0;
}
(VIP) 10) Write a program enter 10 11) Write a program enters 10 12) Write a program enter
values in 1st array kept all values of values in 1st array and search any temperature of week, print out
1st in 2nd array in reverse. Display item by using linear search. maximum and minimum temp.
sum of both array in 3rd array. Print # include <iostream> # include <iostream>
3rd array on screen. using namespace std; using namespace std;
# include <iostream> int main() int main()
using namespace std; { {
int main() int A[0], I, n, loc=-1; int Temp[7], i, max, min;
{ for(i=0; i<=9; i++) for(i=0; i<=6; i++)
int A[10], B[10], C[10], I, j=9; { {
for(i=0; i<=9; i++) cout<<”enter any number”; cout<<”enter temperature”;
{ cin>>A[i]; cin>>temp[i];
cout<<”enter values in 1st array”; } }
cin>>A[i]; cout<<”enter number for search”; min=temp[0];
B[j]=A[i]; cin>>n; max=temp[0];
j-- { for(i=0; i<=6; i++)
} for(i=0; i<=9; i++) {
for(i=0; i<=9; i++) if(A[i]==n) If(temp[i]<min)
{ loc=I; min=temp[i];
C[i]=A[i]+B[i] } if(temp[i]>max)
cout<<c[i]<<endl; If(loc==-1) max=temp[i];
} cout<<”item is not found=”; }
return0; else cout<<” maximum temp=”<<max;
} cout<<”item is found al location”; cout<<”minimum temp=”<<min;
return0; return0;
} }
Ch# 5 (Structure)
1) Write a program enter roll no, 2) Write a program enter record of 3) Write a program enter
marks, and name of 100 students in a 100 students having roll no, marks, record of 100 books having
structure named students. Display all and nameprint out the records of book name, Auther name, total
records on the screen. those students who got more than pages and book price print out
# include <iostream> 50 marks. the records of all books having
using namespace std; # include <iostream> more than 100 pages and less
int main() using namespace std; than 1000/= price.
{ int main() # include <iostream>
Struct STUDENT { using namespace std;
{ Struct STUDENT int main()
int roll, marks; { {
char name[50]; int roll, marks; Structure BOOK
}; char name[50]; {
STUDENT A[100]; }; int page, price;
int i; Student A[100]; char [Link][100], Auther
for(i=0; i<=99; i++) int i; name[100];
{ for(i=0; i<=99; i++) };
cout<<”enter roll no, marks and name { Book A[100];
of students”; cout<<”enter roll no, marks and name int i;
cin>>A[i].roll>>A[i].name>>A[i].marks; of students”; for(i=0; i<=99; i++)
} cin>>A[i].roll>>A[i].name>>A[i].marks; {
for(i=0; i<=99; i++) } cout<<”enter boom name, Auther
{ for(i=0; i<=99; i++) name, Price, Pages”;
cout<<”Roll no=”<<A[i].roll; { cin>>A[i].[Link]>>A[i].Author
cout<<” Name=”<<A[i].name; If(A[i].marks>50) >>A[i].price>>A[i].pages >>A[i];
cout<<”Marks=”<<A[i].marks; { }
} cout<<”Roll no=”<<A[i].roll; for(i=0; i<=99; i++)
return0; cout<<” Name=”<<A[i].name; {
} cout<<”Marks=”<<A[i].marks; If(A[i].page>100&&A[i].price<1000)
} {
} cout<<”Book name=”<<A[i][Link];
cout<<” Author name=”<<A[i][Link];
return0; cout<<”Price=”<<A[i].price;
} cout<<” Total pages=”<<A[i]pages<<endl;
}
}
return0;
}
Ch# 6 (Function)
1) Write a program enter two values 2) Write a program enter marks of 3) Write a program enter
from the user pass by function. Print three subjects and total marks pass power & base from user pass by
out sum of that function. by a function, print out the %age in pointers, print out the result in
(Value Not Return) main function. (Return) main function (Pointer).
# include <iostream> # include <iostream> # include <iostream>
using namespace std; using namespace std; using namespace std;
void sum (int, int); float PERCENTAGE (int, int , int , int ); void PB(int, int , int *)
int main() int main() int main()
{ { {
int a, b; int s1, s2, s3, tm; int p, b, R=1;
cout<<”enter two values”; float p; cout<<”enter power & base”;
cin>>a>>b; cout<<”enter marks of three subjects cin>>P>>B;
sum (a,b); and total marks=”; PB(p, b , & R);
return 0; cin>>s1>>s2>>s3>>tm; cout<<”result=”<<R;
} p=PERCENTAGE (s1, s2, s3, tm); return0;
void sum (int a, int b) cout<<”percentage=”<<p; }
{ return0; void PB(int P, int B, int *R)
Int s; } {
s=a=b; float PERCENTAGE (int s1, int s2, int i;
cout<<”sum of values=”<<s; int s3, int tm) for(i=1, i<=P, i++)
return0; { *R=*R *i;
} float per; }
per=(s1+s2+s3)*100/tm;
return p;
}