You are on page 1of 99

SAOOD HANIF

BS(IT)
BIIT
Sequential Structured Programs
Program#1:
A program that takes input of two floating point numbers from userthen calculates and prints
their sum.
Output:
Enter first number: 23.21
Enter second number: 16.18
Sum of 23.21 and 16.18 is 39.39
Solution:
int _tmain(int argc, _TCHAR* argv[])

float var1,var2;

cout<<"Enter first number:";

cin>>var1;

cout<<"Enter second number:";

cin>>var2;

cout<<"Sum of "<<var1<<" and "<<var2<<" is "<<var1+var2<<endl;

return 0;

Program#2:
A program that takes input of two integer type numbers from user then calculates and prints
their sum, difference, product and division (in point) result.
Output:
Enter first number: 13
Enter second Number: 7
Sum of 13 and 7 is: 20
Difference of 13 and 7 is: 6
Product of 13 and 7 is: 91
Dividing 13 by 7 results: 1.85714
Solution:
int _tmain(int argc, _TCHAR* argv[])

float var1,var2;

SAOOD HANIF
BS(IT)
BIIT
cout<<"Enter first number:";

cin>>var1;

cout<<"Enter second number:";

cin>>var2;

cout<<"Sum of "<<var1<<" and "<<var2<<" is "<<var1+var2<<endl;

cout<<"Sum of "<<var1<<" and "<<var2<<" is "<<var1-var2<<endl;

cout<<"Sum of "<<var1<<" and "<<var2<<" is "<<var1*var2<<endl;

cout<<"Sum of "<<var1<<" and "<<var2<<" is "<<var1/var2<<endl;

return 0;

Program#3:
A program that takes input of four floating point numbers from user then calculates and prints
their average.
Output:
Enter four numbers:12.81
34.09
65.12
19.67
Average of 12.81, 34.09, 65.12 and 19.67 is 32.9225
Solution:
int _tmain(int argc, _TCHAR* argv[])

float var1,var2,var3,var4,avg;

cout<<"Enter four numbers:";

cin>>var1;

cin>>var2;

cin>>var3;

cin>>var4;

avg=(var1+var2+var3+var4)/4;

cout<<"Average of "<<var1<<" , "<<var2<<" , "<<var3<<" , "<<var4<<" is "<<avg<<endl;

return 0;

SAOOD HANIF
BS(IT)
BIIT
}

Program#4:
A program that takes input of a floating point number from user then calculates and prints its
square and inverse. ( square = x*x inverse = 1/x ).
Output:
Enter a number: 18.65
Square of 18.65 is: 347.8225
Inverse of 18.65 is:0.053619
Solution:
int _tmain(int argc, _TCHAR* argv[])

float var1;

cout<<"Enter a number:";

cin>>var1;

cout<<"Square of "<<var1<<" is: "<<var1*var1<<endl;

cout<<"Inverse of "<<var1<<" is: "<<1/var1<<endl;

return 0;

Program#5:
A program that takes input of length and width of a rectangle from user then calculates and
prints its area and perimeter. (Area=length*width, perimeter=2*( length + width ) ).
Output:
Enter length of rectangle: 15.6
Enter width of rectangle: 21
Area of rectangle is:327.6
Perimeter of rectangle is: 18.3
Solution:
int _tmain(int argc, _TCHAR* argv[])

float var1,var2;

cout<<"Enter lenght of rectangle:";

cin>>var1;

cout<<"Enter widht of rectangle:";

cin>>var2;

SAOOD HANIF
BS(IT)
BIIT
cout<<"Area of rectangle: "<<var1*var2<<endl;

cout<<"Perimeter of rectangle: "<<(2*(var1+var2))<<endl;

return 0;

Program#6:
A program that takes input of a circle radius from user then calculates and prints the circle’s
area, diameter and circumference. (Area= π r2, circumference=2πr , diameter=2r. Use
constant value 3.14159 for п)
Output:
Enter radius of circle: 13
Area of circle is: 530.92871
Diameter of circle is: 26
Circumference of circle is: 81.68134
Solution:
int _tmain(int argc, _TCHAR* argv[])

float r, a, d, c,pie;

cout<<"Enter radius of circle:";

cin>>r;

pie=3.14159;

a=pie*(r*r);

c=2*(pie*r);

d=2*r;

cout<<"Area of circle is :"<<a<<endl;

cout<<"Diameter of circle is :"<<d<<endl;

cout<<"Circumference of circle is :"<<c<<endl;

return 0;

Program#7:
A program that reads length and width of a rectangular block, length and width of a house
from user, house is enclosed inside the block, calculate and print the cutting/moving area.

Block Moving area


House

SAOOD HANIF
BS(IT)
BIIT
Output:
Enter length of Block: 22
Enter width of Block: 35
Enter length of House: 15
Enter width of House: 20
Moving area is: 470
Solution:
int_tmain(intargc, _TCHAR* argv[])
{

int bl,bw,hl,hw,house,block,res;

cout<<"Enter Lenght of a block: ";

cin>>bl;

cout<<"Enter Width of a block: ";

cin>>bw;

cout<<"Enter Lenght of a house: ";

cin>>hl;

cout<<"Enter width of a house :";

cin>>hw;

block=bl*bw;

house=hl*hw;

res=block-house;

cout<<"Moving area is: "<<res<<endl;

return 0;

Program#8:
A program that reads a temperature value in degree centigrade, then converts and outputs its
Fahrenheit equivalent, and vice versa.
Output:
Enter temperature in degree centigrade: 23
Fahrenheit equivalent is: 73.4
Enter temperature in Fahrenheit: 42

SAOOD HANIF
BS(IT)
BIIT
Centigrade equivalent is: 5.55556
Solution:
int _tmain(int argc, _TCHAR* argv[])

float c,f;

cout<<"Enter temp in degree centigrade: ";

cin>>c;

f=c*9/5+32;

cout<<"Fahrenheit equivalent is: "<<f<<endl;

cout<<"Enter temp in Fahrenheite: ";

cin>>f;

c=(f-32)*5/9;

cout<<"Centigrade equivalent is: "<<c<<endl;

return 0;

Program#9:
A program that reads two integer type variables thenswaps value in those variables.
Output:
Enter value of x: 15
Enter value of y: 27
After swapping
Value of x is: 27
Value of y is: 17
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x, y,temp;

cout<<"Enter value of x: ";

cin>>x;

cout<<"Enter value of y: ";

cin>>y;

SAOOD HANIF
BS(IT)
BIIT
cout<<"After swapping"<<endl;

temp=x;

x=y;

y=temp;

cout<<"Value of x is: "<<x<<endl;

cout<<"Value of y is: "<<y<<endl;

return 0;

Program#10:
A program that reads two integer type variables then swaps value in those variables without
using third variable.
Output:
Enter value of x: 15
Enter value of y: 27
After swapping
Value of x is: 27
Value of y is: 17
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x, y;

cout<<"Enter value of x: ";

cin>>x;

cout<<"Enter value of y: ";

cin>>y;

cout<<"After swapping"<<endl;

x=x+y;

y=x-y;

x=x-y;

cout<<"Value of x is: "<<x<<endl;

cout<<"Value of y is: "<<y<<endl;

SAOOD HANIF
BS(IT)
BIIT
return 0;

Program#11:
A program that inputsuser’s year of birth, month of birth, the current year and the current
month then computes and prints user’s age in years and in months (months range is 1-----12).
Output:
Enter year of birth: 1991
Enter Month of Birth: 4
Enter current year: 2010
Enter current month: 1
Years=18
Months=9
Solution:
int _tmain(int argc, _TCHAR* argv[])

int birthy, birthm, currenty, currentm, years, months;

cout<<"Enter year of birth: ";

cin>>birthy;

cout<<"Enter Month of birth: ";

cin>>birthm;

cout<<"Enter current year: ";

cin>>currenty;

cout<<"Enter current month: ";

cin>>currentm;

years=((currenty*12)+currentm)-((birthy*12)+birthm);

months=years%12;

years=years/12;

cout<<"Years: "<<years<<endl;

cout<<"months: "<<months<<endl;

return 0;

SAOOD HANIF
BS(IT)
BIIT
Program#12:
A program that takes inputof floating point variables for base, hypotenuse and perpendicular
of a triangle, then computes its area. (Area=(B*H)/2, perimeter=B+H+P)
Output:
Enter Base of a triangle: 12.4
Enter Height of a triangle: 21
Area of triangle is: 130.2
Solution:
int _tmain(int argc, _TCHAR* argv[])

float base, height, area;

cout<<"Enter Base of a triangle: ";

cin>>base;

cout<<"Enter Height of a triangle: ";

cin>>height;

area=(base*height)/2;

cout<<"Area of triangle is: "<<area<<endl;

return 0;

Program#13:
A program that takes input of two floating point variables for base and height of a triangle,
then computes length of hypotenuse.

Height hypotenuse

Base
Output:
Enter Base of a triangle: 12
Enter Height of a triangle: 19
Length of hypotenuse is: 22.47
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
float b,h,hy;

SAOOD HANIF
BS(IT)
BIIT
cout<<"Enter Base of a triangle: ";
cin>>b;
cout<<"Enter Height of a triangle: ";
cin>>h;
b=b*b;
h=h*h;
hy=sqrt(b+h);
cout<<"Length of hypotenuse is: "<<hy<<endl;

return 0;
}
Program#14:
A program that reads a, b and c, values in quadratic formula ax2+bx+c=0 and prints its roots
−𝑏±√𝑏 2 −4𝑎𝑐
as 2𝑎
Output:
Enter value of a: 5
Enter value of b: 8
Enter value of c: 6
Roots for quadratic equations are and .
Solution:

Program#15:
A program that takes input of a four-digit number then calculates and prints sum of the digits
and reverse of that number.
Output:
Enter a four-digit number: 4361
Sum of 4, 3, 6 and 1 is: 14
Reverse of 4361 is: 1634
Solution:
int _tmain(int argc, _TCHAR* argv[])

int a, b, c, d, sum;

cout<<"Enter four-digit number: ";

cin>>a>>b>>c>>d;

sum=a+b+c+d;

cout<<"Sum of "<<a<<", "<<b<<", "<<c<<", "<<d<<" is: "<<sum<<endl;

cout<<"Reverse of "<<a<<b<<c<<d<<" is: "<<d<<c<<b<<a<<endl;

return 0;

SAOOD HANIF
BS(IT)
BIIT
Program#16:
A program that takes input of a five-digit binary number then calculates and prints its decimal
and octal equivalent.
Output:
Enter a five-digit binary number: 10110
Octal notation for 10110 is: 26
Decimal notation for 10110 is:22
Solution:

Program#17:
A program that takes input of a floating point number of centimeters, print out the equivalent
number of feet (integer) and inches (float). Assume 2.54 centimeters per inch, and 12 inches
per foot.
Output:
Enter value in centimeters: 333.3
333.3 centimeters is 10 feet and 11.2 inches
Solution:
int _tmain(int argc, _TCHAR* argv[])

int inches, value, k =2.54;

int feet;

cout<<"Enter value in centimeters: ";

cin>>value;

inches=value/2.54;

feet=inches/12;

inches=inches%12;

cout<<value<<" centimeters is "<<feet<<" feet and "<<inches<<" inches "<<endl;

return 0;

Program#18:
Program that takes input of an integer number as seconds, print as output the equivalent time
in hours, minutes and seconds.
Output:
Enter time in seconds: 7322
7322 seconds is 2hours 2 minutes and 2 seconds.
Solution:
int_tmain(intargc, _TCHAR* argv[])
{

SAOOD HANIF
BS(IT)
BIIT
int x,b,hours,min,sec;
cout<<"Enter time in sec ";
cin>>sec;
b=sec;
hours=sec/3600;
min=sec%3600;
min=min/60;
sec=sec%3600;
sec=sec%60;
cout<<b<<" Seconds is "<<hours<<" hours "<<min<<" minutes "<<sec<<"
seconds"<<endl;
return 0;
}
Program#19:
A program that takes input of a character type variable then prints its ASCII equivalent.
Output:
Enter a character: H
Its ASCII value is: 72
Solution:
int _tmain(int argc, _TCHAR* argv[])

char ch;

int x;

cout<<"Enter a character: ";

cin>>ch;

x=ch;

cout<<"Its ASCII value is: "<<x<<endl;

return 0;

Program#20:
A program that takes input of an integer type variable as ASCII value then prints its
equivalent character.
Output:
Enter anyASCII value: 72
Its equiva0lent character is: H
Solution:
int _tmain(int argc, _TCHAR* argv[])

char ch;

SAOOD HANIF
BS(IT)
BIIT
int x;

cout<<"Enter any ASCII vaue: ";

cin>>x;

ch=x;

cout<<"Its equivalent character is: "<<ch<<endl;

return 0;

Program#21:
A program that takes input of a character as small alphabet then prints it in capital.
Output:
Enter an alphabet in small: f
Capital alphabet is: F
Solution:
int _tmain(int argc, _TCHAR* argv[])

char ch;

int x;

cout<<"Enter any aplhabet in small: ";

cin>>ch;

ch=ch-32;

cout<<"Its capital is: "<<ch<<endl;

return 0;

Program#22:
A program that takes input of a character as Capital alphabet then prints it in small.
Output:
Enter an alphabet in capital: F
Small alphabet is: f
Solution:
int _tmain(int argc, _TCHAR* argv[])

SAOOD HANIF
BS(IT)
BIIT
char ch;

int x;

cout<<"Enter any aplhabet in capital: ";

cin>>ch;

ch=ch+32;

cout<<"Its small is: "<<ch<<endl;

return 0;

Selection Structured Programs


Program#23:
A program that takes input of a number from user then checks and prints whether is it even or
odd.
Output:
Enter a number: 12
12 is an even number
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int x;
cout<<"Enter a number: ";
cin>>x;
if(x%2==0)
cout<<x<<" is an even number"<<endl;
if(x%2!=0)
cout<<x<<" is an odd number"<<endl;
return 0;
}
Program#24:
A program that takes input of a number from user then checks and prints whether is it
negative or positive.
Output:
Enter a number: -13
-13 is a negative number
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int x;
cout<<"Enter a number: ";
cin>>x;
if(x<0)
cout<<x<<" is an negative number"<<endl;
else
if(x>0)
cout<<x<<" is an positive number"<<endl;

SAOOD HANIF
BS(IT)
BIIT
else
cout<<"Invalid number"<<endl;
return 0;
}
Program#25:
A program that takes input of four numbers from user then prints the largest and smallest
numbers.
Output:
Enter first numbers: 12
Enter second number: 85
Enter third number: 43
Enter fourth number: 52
85 is the largest number
12 is the smallest number
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int a,b,c,d,max,min;
cout<<"Enter 1st number: ";
cin>>a;
cout<<"Enter 2nd number: ";
cin>>b;
cout<<"Enter 3rd number: ";
cin>>c;
cout<<"Enter 4th number: ";
cin>>d;
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
if(max<d)
max=d;
min=a;
if(min>b)
min=b;
if(min>c)
min=c;
if(min>d)
min=d;
cout<<max<<" is the largest number"<<endl;
cout<<min<<" is the smallest number"<<endl;
return 0;
}
Program#26:
A program that reads value for student’s percentage then prints Grade according to it.
Percentage Range Grade
91 – 100 A+
81 – 90 A
71 – 80 B+
61 – 70 B
51 – 60 C
0 – 50 F
Output:

SAOOD HANIF
BS(IT)
BIIT
Enter student’s percentage: 65
Grade on 65 % is: B
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
float a;
cout<<"Enter student's percentage: ";
cin>>a;
if(a>=0&&a<=50)
cout<<"Grade on "<<a<<"% is: F "<<endl;
if(a>=51&&a<=60)
cout<<"Grade on "<<a<<"% is: C "<<endl;
if(a>=61&&a<=70)
cout<<"Grade on "<<a<<"% is: B "<<endl;
if(a>=71&&a<=80)
cout<<"Grade on "<<a<<"% is: B+ "<<endl;
if(a>=81&&a<=90)
cout<<"Grade on "<<a<<"% is: A "<<endl;
if(a>=91&&a<=100)
cout<<"Grade on "<<a<<"% is: A+ "<<endl;

return 0;
}
Program#27:
Program that takes values for seconds, minutes and hours for two times and show their sum
also check invalid time entered by user.
( Invalid time can be sec= - 45, minutes= 78 etc .)
Output:
Enter time in hours: min: sec
22 : 45 : 38
Enter time in hours: min: sec
65 : 52 : 29
Addition of times is hour: min: sec
88 : 38 : 07
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int hours,min,sec,hours1,min1,sec1,shours,smin,ssec;
char ch;
cout<<"Enter time in Hours: min: sec ";
cin>>hours>>ch>>min>>ch>>sec;
cout<<"Enter time in Hours: min: sec ";
cin>>hours1>>ch>>min1>>ch>>sec1;
shours=hours+hours1;
smin=min+min1;
ssec=sec+sec1;
shours=shours*3600;
smin=smin*60;
ssec=ssec+shours+smin;
shours=ssec/3600;
smin=ssec%3600;
smin=smin/60;
ssec=ssec%3600;
ssec=ssec%60;
cout<<"Addition of time is Hours: min: sec ";

SAOOD HANIF
BS(IT)
BIIT
cout<<shours<<": "<<smin<<": "<<ssec<<endl;
return 0;
}
Program#28:
A program that takes input of number from user then prints whether number is divisible by 2
or by 5 or by both or by none.
Output:
Enter a number: 20
20 is divisible by both 2 and 5
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int x;
cout<<"Enter a number: ";
cin>>x;
if(x%2==0&&x%5==0)
cout<<x<<" is divisible by both 2 and 5"<<endl;
else
if(x%2==0)
cout<<x<<" is divisible by 2"<<endl;
else
if(x%5==0)
cout<<x<<" is divisible by 5"<<endl;
else
cout<<x<<" is not divisible by 2 or 5"<<endl;

return 0;
}
Program#29:
A program that takes input of two integer type variables from user then prints whether first
number is a multiple of second, or second number is a multiple of first number, or both are
not multiples of each other.
Output:
Enter first number: 5
Enter second number: 20
5 is a multiple of 20
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int x,y;
cout<<"Enter a first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;
if(x>y)
{
if(x%y==0)
cout<<y<<" is a multiple of "<<x<<endl;
}
else
if(x<y)
{
if(y%x==0)
cout<<x<<" is a multiple of "<<y<<endl;

SAOOD HANIF
BS(IT)
BIIT
}
if(x==y)
cout<<x<<" and "<<y<<" are multiples of each other"<<endl;
else
cout<<x<<" and "<<y<<" are multiples are not multiples of each
other"<<endl;
return 0;
}

Program#30:
A program that reads temperature value, and a choice from user. If user enters ‘C’ then
temperature must be converted into degree Centigrade, if user enters ‘F’ temperature must be
converted into Fahrenheit.
Output:
Enter temperature: 42
Enter your choice(C for Centigrade and F for Fahrenheit): C
Temperature in Centigrade is: 5.55556
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
char ch;
float temp,f,c;
cout<<"Enter temperature: ";
cin>>temp;
cout<<"Enter your choice (C for centigrade and F for Fahrenheit): ";
cin>>ch;
if(ch=='f'||ch=='F')
{
f=temp*9/5+32;
cout<<"Temperature in Fahrenheit is: "<<f<<endl;
}
if(ch=='c'||ch=='C')
{
c=(temp-32)*5/9;
cout<<"Temperature in Centigrade is: "<<c<<endl;
}
return 0;
}
Program#31:
A program that reads 5 values and then determines and outputs whether the sequence entered
is sorted in ascending order, descending order, or is unordered
Output:
Enter the first integer: 15
Enter the second integer: 8
Enter the third integer: 6
Enter the fourth integer: 5
Enter the fifth integer: 2
Your sequence is sorted in descending order
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int a, b, c, d,e;
cout<<"Enter the First Integer: ";
cin>>a;

SAOOD HANIF
BS(IT)
BIIT
cout<<"Enter the 2nd Integer: ";
cin>>b;
cout<<"Enter the 3rd Integer: ";
cin>>c;
cout<<"Enter the 4th Integer: ";
cin>>d;
cout<<"Enter the 5th Integer: ";
cin>>e;
if(a<=b&&b<=c&&c<=d&&d<=e)
cout<<"Your Sequence is Sorted in ascending order"<<endl;
else
{
if(a>=b&&b>=c&&c>=d&&d>=e)
cout<<"Your Sequence is Sorted in descending order"<<endl;
else
cout<<"Your Sequence is Unsorted"<<endl;

return 0;
}
Program#32:
A program that takes input of a five-digit number from user then determines whether it is a
Numerical Palindrome. A Numerical Palindrome is a number that is the same forward and
backward, such as 43134
Output:
Enter a five-digit number: 61216
61216 is a numerical palindrome
Solution:
int _tmain(int argc, _TCHAR* argv[])
{
int x,rem=1,rev=0,b;
cout<<"Enter a five digit number: ";
cin>>x;
b=x;
while(b>0)
{
rem=b%10;
b=b/10;
rev=rev*10+rem;
}
if(rev==x)
cout<<x<<" is a numerical palindrome"<<endl;
else
cout<<x<<" is not a numerical palindrome"<<endl;

return 0;
}
Program#33:
A program that takes input of an eight-digit binary number and user choice then performs
conversion according to user choice User choice can be ‘H’ for Hexadecimal, ‘O’ for Octal
and ‘D’ for Decimal equivalent conversion.
Output:
Enter an eight-digit binary number: 10110110
Enter your choice (O for Octal for Decimal for Hexadecimal): H

SAOOD HANIF
BS(IT)
BIIT
Hexadecimal notation for 10110110 is: B6
Solution:

Program#34:
A program that computes and prints the height of a person in feet if the value of character
variable height _unit is ‘F’ do so in meters if the value is ‘M’. Assume that the value of
variable Height is expressed in inches (1 foot is 12 inches, and one inch is 0.0 254 meters)
Output:
Enter height in inches: 62
Enter height unit: F
Height in Feet is: 5 feet 2 inches
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int inches,feet;
float meters;
char ch;
cout<<"Enter Height in inches: ";
cin>>inches;
cout<<"Enter height unit: ";
cin>>ch;
if(ch=='f'||ch=='F')
{
feet=inches/12;
inches=inches%12;
cout<<"Height in Feet is: "<<feet<<" feet "<<inches<<" inches"<<endl;
}
if(ch=='m'||ch=='M')
{
meters=inches*0.0254;
cout<<"Height in Meters is: "<<meters<<" meters "<<endl;
}
return 0;
}
Program#35:
Program that takes input of a character type variable then determines and prints whether it
contains a small alphabet letter, or a capital alphabet letter, or a number, or a special character
from keyboard. (Use ASCII codes)
Small alphabet letters (abcdefghijklmnopqrstuvwxyz)
Capital alphabet letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
Numbers (0123456789)
Special characters ( ; , “ \ | @ % $ > ? etc )
Output:
Enter a character from keyboard: }
} is a special character on keyboard
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int x;
char ch;
cout<<"Enter a character from keyboard: ";
cin>>ch;
x=ch;

SAOOD HANIF
BS(IT)
BIIT
if(x>=97&&x<=122)
cout<<ch<<" is a small alphabet on keyboard"<<endl;
if(x>=65&&x<=90)
cout<<ch<<" is a capital alphabet on keyboard"<<endl;
if(x>=48&&x<=57)
cout<<ch<<" is a numbers on keyboard"<<endl;
if((x>=33&&x<=47)||(x>=58&&x<=64)||(x>=91&&x<=96)||(x>=123&&x<=126))
cout<<ch<<" is a special character on keyboard"<<endl;

return 0;
}
Program#36:
A program that prompts the user to input x, y coordinates of a point in a Cartesian plane. The
program should then output a message indicating whether point is origin, is located on x or y
axis or appears in a particular quadrant.
e.g. (0,0) is on origin
(4,0) is on x-axis
(0, -3) is on y-axis x=0

(-,+) (+,+)

(0,0) y=0

(-,-) (+,-)

Output:
Enter value for x: - 6
Enter value for y: 4
( - 6, 4 ) lies in second quadrant
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int x,y;
cout<<"Enter value for x: ";
cin>>x;
cout<<"Enter value for y: ";
cin>>y;
if(x==0&&y==0)
cout<<x<<" and "<<y<<" is on origin"<<endl;
if(x>0&&y>0)
cout<<x<<" and "<<y<<" lies in first quadrant"<<endl;
if(x<0&&y>0)
cout<<x<<" and "<<y<<" lies in second quadrant"<<endl;
if(x<0&&y<0)
cout<<x<<" and "<<y<<" lies in third quadrant"<<endl;
if(x>0&&y<0)
cout<<x<<" and "<<y<<" lies in fourth quadrant"<<endl;

return 0;
}
Program#37:
A program that takes input of three sides of a triangle then checks and prints whether these
sides form an equilateral, isosceles or right-angled triangle.

SAOOD HANIF
BS(IT)
BIIT
Output:
Enter first angle of triangle: 50
Enter second angle of triangle: 50
Enter third angle of triangle: 80
Given sides form an isosceles Triangle
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int a,b,c;
cout<<"Enter first angle of triangle: ";
cin>>a;
cout<<"Enter 2nd angle of triangle: ";
cin>>b;
cout<<"Enter 3rd angle of triangle: ";
cin>>c;
if(a+b+c==180)
{
if(a==b&&b!=c&&a!=c)
cout<<"Given Sides forn an isosceles Triangle"<<endl;
if(a==b&&b==c&&a==c)
cout<<"Given Sides forn an equilateral Triangle"<<endl;
if(a==90&&b==90)
cout<<"Given Sides forn an right-angle Triangle"<<endl;
}
else
cout<<"Invalid input"<<endl;
return 0;
}
Program#38:
A program that reads an amount as an integer then determines and prints the minimum
number of notes of 50,20,10,5,1 denominations in it. For example, if the amount is 179
rupees, your program should print that it contains three 50, one 20, zero 5, and four 1 rupee
notes.
Output:
Enter amount in rupees: 76
76 contains
No. of 50 notes: 1
No. of 20 notes: 1
No. of 5 notes: 1
No. of 1 notes: 1
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int a,n50,n20,n5,n1;
cout<<"Enter amount in Rupees: ";
cin>>a;
n50=a/50;
n20=a%50;
n20=n20/20;
n5=a%50;
n5=n5%20;
n5=n5/5;
n1=a%50;
n1=n1%20;

SAOOD HANIF
BS(IT)
BIIT
n1=n1%5;
n1=n1/1;
cout<<"No. of 50 notes: "<<n50<<endl;
cout<<"No. of 20 notes: "<<n20<<endl;
cout<<"No. of 5 notes: "<<n5<<endl;
cout<<"No. of 1 notes: "<<n1<<endl;
return 0;
}

Multi way Selection using switch


Program#39:
A program that takes input of a number as week day (1-7) then prints week day accordingly
Output:
Enter the number for a week day: 3
The week day on number 3 is Wednesday
Solution:
int _tmain(int argc, _TCHAR* argv[])

char ch;

cout<<"Enter the number for a week day: ";

cin>>ch;

switch(ch)

case '1':

cout<<"The week day on number "<<ch<<" is Monday"<<endl;break;

case '2':

cout<<"The week day on number "<<ch<<" is Tuesday"<<endl;break;

case '3':

cout<<"The week day on number "<<ch<<" is Wednesday"<<endl;break;

case '4':

cout<<"The week day on number "<<ch<<" is Thursday"<<endl;break;

case '5':

cout<<"The week day on number "<<ch<<" is Friday"<<endl;break;

case '6':

SAOOD HANIF
BS(IT)
BIIT
cout<<"The week day on number "<<ch<<" is Saturday"<<endl;break;

case '7':

cout<<"The week day on number "<<ch<<" is Sunday"<<endl;break;

default:

cout<<"Invalid Number"<<endl;break;

return 0;

Program#40:
A program that takes input of a number then prints whether is it even or odd
Output:
Enter a number: 28
28 is an even number
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y;

cout<<"Enter a number: ";

cin>>x;

if(x%2==0)

y=1;

else

y=2;

switch(y)

case 1:

cout<<x<<" is a even number"<<endl;break;

case 2:

cout<<x<<" is a odd number"<<endl;break;

SAOOD HANIF
BS(IT)
BIIT
}

return 0;

Program#41:
A program that takes input of a number then prints whether is it negative or positive
Output:
Enter a number: 65
65 is a positive number
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y;

cout<<"Enter a number: ";

cin>>x;

if(x>0)

y=1;

if(x<0)

y=2;

switch(y)

case 1:

cout<<x<<" is a Positive number"<<endl;break;

case 2:

cout<<x<<" is a Negivtive number"<<endl;break;

return 0;

Program#42:
A program that takes input of a number as month (1-12) then prints month accordingly

SAOOD HANIF
BS(IT)
BIIT
Output:
Enter the number of month: 4
The month is April
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y;

cout<<"Enter the number of month: ";

cin>>x;

switch(x)

case 1:

cout<<x<<" The month is jan"<<endl;break;

case 2:

cout<<x<<" The month is Feb"<<endl;break;

case 3:

cout<<x<<" The month is March"<<endl;break;

case 4:

cout<<x<<" The month is April"<<endl;break;

case 5:

cout<<x<<" The month is May"<<endl;break;

case 6:

cout<<x<<" The month is June"<<endl;break;

case 7:

cout<<x<<" The month is July"<<endl;break;

case 8:

cout<<x<<" The month is August"<<endl;break;

case 9:

SAOOD HANIF
BS(IT)
BIIT
cout<<x<<" The month is Sept"<<endl;break;

case 10:

cout<<x<<" The month is October"<<endl;break;

case 11:

cout<<x<<" The month is Nov"<<endl;break;

case 12:

cout<<x<<" The month is Dec"<<endl;break;

default:

cout<<"invalid"<<endl;break;

return 0;

Program#43:
A program that takes input of an alphabet then prints whether it is a vowel or consonant.
Output:
Enter an alphabet: k
k is a consonant
Solution:
int _tmain(int argc, _TCHAR* argv[])

char ch;

cout<<"Enter the number of month: ";

cin>>ch;

switch(ch)

case 'a':

cout<<ch<<" is a vowel"<<endl;break;

default:

SAOOD HANIF
BS(IT)
BIIT
cout<<ch<<" is a consonant"<<endl;break;

return 0;

Program#44:
A program that takes two integer type variables and an arithmetic operator (+, - , *, /, %) as
input then performs arithmetic operation on input numbers accordingly.
Output:
Enter first number: 28
Enter second number: 10
Enter arithmetic operator: /
28 /10=2
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x, y;

char ch;

cout<<"Enter first number: ";

cin>>x;

cout<<"Enter second number: ";

cin>>y;

cout<<"Enter arithmetic operator: ";

cin>>ch;

switch(ch)

case '+':

cout<<x<<ch<<y<<"="<<x+y<<endl;break;

case '-':

cout<<x<<ch<<y<<"="<<x-y<<endl;break;

case '/':

SAOOD HANIF
BS(IT)
BIIT
cout<<x<<ch<<y<<"="<<x/y<<endl;break;

case '*':

cout<<x<<ch<<y<<"="<<x*y<<endl;break;

case '%':

cout<<x<<ch<<y<<"="<<x%y<<endl;break;

default:

cout<<" Invalid Operator"<<endl;break;

return 0;

Program#45:
A program that takes input of an alphabet then prints whether it is a capital alphabet or not.
Output:
Enter an alphabet: q
q is not a capital alphabet
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x;

char ch;

cout<<"Enter an alphabet: ";

cin>>ch;

if(ch>=65&&ch<=90)

x=1;

if(ch>=97&&ch<=122)

x=2;

switch(x)

case 1:

SAOOD HANIF
BS(IT)
BIIT
cout<<ch<<" is a capital alphabet"<<endl;break;

case 2:

cout<<ch<<" is not a capital alphabet"<<endl;break;

default:

cout<<" Invalid Character"<<endl;break;

return 0;

Program#46:
A program that takes input of an alphabet then prints whether it is a small alphabet or not.
Output:
Enter an alphabet: q
q is a small alphabet
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x;

char ch;

cout<<"Enter an alphabet: ";

cin>>ch;

if(ch>=65&&ch<=90)

x=1;

if(ch>=97&&ch<=122)

x=2;

switch(x)

case 1:

cout<<ch<<" is not a small alphabet"<<endl;break;

case 2:

SAOOD HANIF
BS(IT)
BIIT
cout<<ch<<" is a small alphabet"<<endl;break;

default:

cout<<" Invalid Character"<<endl;break;

return 0;

Program#47:
A program that takes input of a capital alphabet then prints it in small alphabet.
Output:
Enter capital alphabet: Y
The small alphabet is y
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x;

char ch;

cout<<"Enter capital alphabet: ";

cin>>ch;

if(ch>=65&&ch<=90)

x=1;

switch(x)

case 1:

ch=ch+32;

cout<<"The small alphabet is "<<ch<<endl;break;

default:

cout<<" Invalid Character"<<endl;break;

return 0;

SAOOD HANIF
BS(IT)
BIIT
}

Program#48:
A program that takes input of a small alphabet then prints it in capital alphabet.
Output:
Enter small alphabet: e
The capital alphabet is E
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x;

char ch;

cout<<"Enter capital alphabet: ";

cin>>ch;

if(ch>=97&&ch<=122)

x=1;

switch(x)

case 1:

ch=ch-32;

cout<<"The Capital alphabet is "<<ch<<endl;break;

default:

cout<<" Invalid Character"<<endl;break;

return 0;

Program#49:
A program that takes input of an alphabet then prints it in small if it was capital and prints it
in capital if it was a small alphabet.
Output:
Enter an alphabet: g
The entered alphabet is small its capital is G.

SAOOD HANIF
BS(IT)
BIIT
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x;

char ch;

cout<<"Enter capital alphabet: ";

cin>>ch;

if(ch>=65&&ch<=90)

x=1;

if(ch>=97&&ch<=122)

x=2;

switch(x)

case 1:

ch=ch+32;

cout<<"The entered alphabet is capital its small is "<<ch<<endl;break;

case 2:

ch=ch-32;

cout<<"The entered alphabet is small its capital is "<<ch<<endl;break;

default:

cout<<" Invalid Character"<<endl;break;

return 0;

SAOOD HANIF
BS(IT)
BIIT
Repetition Structured Programs
Program#50:
A program that takes input of two numbers from user as a starting and ending then prints all
even numbers between them.
Output:
Enter starting number: 7
Enter ending number: 25
Even numbers between 7 and 15 are
8, 10, 12, 14, 16, 18, 20, 22, 24
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y;

cout<<"Enter starting number: ";

cin>>x;

cout<<"Enter ending number: ";

cin>>y;

cout<<"Even Numbers between "<<x<<" and "<<y<<" are: ";

for(int i=x;i<=y;i++)

if(i%2==0)

cout<<i<<" ";

cout<<endl;

return 0;

Program#51:
A program that takes input of two numbers from user as a starting and ending then prints the
multiples of 3 and 5 between them.
Output:
Enter starting number: 13
Enter ending number: 38

SAOOD HANIF
BS(IT)
BIIT
Multiples of 3 between 13 and 38 are
15, 18, 21, 24, 27, 30, 33, 36
Multiples of 5 between 13 and 38 are:
15, 20, 25, 30, 35
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y;

cout<<"Enter starting number: ";

cin>>x;

cout<<"Enter ending number: ";

cin>>y;

cout<<"Multiples of 3 between "<<x<<" and "<<y<<" are: ";

for(int i=x;i<=y;i++)

if(i%3==0)

cout<<i<<" ";

cout<<endl;

cout<<"Multiples of 5 between "<<x<<" and "<<y<<" are: ";

for(int i=x;i<=y;i++)

if(i%5==0)

cout<<i<<" ";

cout<<endl;

return 0;

SAOOD HANIF
BS(IT)
BIIT
Program#52:
A program that takes input of two numbers from user as a starting and ending then prints
their squares and cubes
Output:
Enter starting number: 4
Enter ending number: 11
Number Squarer Cube
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
Solution:

int _tmain(int argc, _TCHAR* argv[])

int x,y;

cout<<"Enter starting number: ";

cin>>x;

cout<<"Enter ending number: ";

cin>>y;

cout<<"Number Squares Cube "<<endl;

for(int i=x;i<=y;i++)

cout<<i<<"\t"<<i*i<<"\t"<<i*i*i<<endl;

cout<<endl;

return 0;

Program#53:
Program that takes input of an integer type variable from user then calculates and prints its
factorial.
Output:
Enter a number: 6

SAOOD HANIF
BS(IT)
BIIT
Factorial of 6 is 6.5.4.3.2.1=720
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,fact=1;

cout<<"Enter a number: ";

cin>>x;

cout<<"Factorial of "<<x<<" is: ";

for(int i=x;i>=1;i--)

cout<<i<<".";

fact=fact*i;

cout<<"="<<fact<<endl;

return 0;

Program#54:
A program that takes input of two numbers x and y from user then prints xy .
Output:
Enter value for x: 3
Enter value for y: 5
3 raise to the power 5 is: 243
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,p,res=1;

cout<<"Enter value of x: ";

cin>>x;

cout<<"Enter power of x: ";

SAOOD HANIF
BS(IT)
BIIT
cin>>p;

cout<<x<<" raise to the power "<<p<<" is: ";

for(int i=0;i<p;i++)

res=res*x;

cout<<res<<endl;

return 0;

Program#55:
A program that takes input of two integer type variables from user then prints their LCM
(Least Common Multiple).
Output:
Enter first number: 15
Enter second number: 20
LCM of 15 and 20 is: 60
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y,res=1;

cout<<"Enter first number: ";

cin>>x;

cout<<"Enter second number: ";

cin>>y;

cout<<"LCM of "<<x<<" and "<<y<<" is: ";

for(int i=1;i<=x&&i<=y;i++)

if(x%i==0&&y%i==0)

SAOOD HANIF
BS(IT)
BIIT
res=(x*y)/i;

cout<<res<<endl;

return 0;

Program#56:

A program that takes input of two integer type variables from user then prints their GCD
(Greatest Common Divisor).
Output:
Enter first number: 15
Enter second number: 20
GCD of 15and 20 is: 5
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y,res=1;

cout<<"Enter first number: ";

cin>>x;

cout<<"Enter second number: ";

cin>>y;

cout<<"GCD of "<<x<<" and "<<y<<" is: ";

for(int i=1;i<=x&&i<=y;i++)

if(x%i==0&&y%i==0)

res=i;

cout<<res<<endl;

SAOOD HANIF
BS(IT)
BIIT
return 0;

Program#57:
A program that takes input of a number then prints all of its divisors.
Output:
Enter a number: 32
The divisors of 32 are 1,2,4,8,16,32
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y,res=1;

cout<<"Enter first number: ";

cin>>x;

cout<<"The divisor of "<<x<<" are "<<" is: ";

for(int i=1;i<=x;i++)

if(x%i==0)

cout<<i<<”, “;

cout<<endl;

return 0;

Program#58:
A program that reads a positive integer then determines and outputs the largest power of 2
that is less than or equal to the given number.
Output:
Enter a number: 35
2 raise to the power 5 is 32 which is less than or equal to 35
Solution:
int _tmain(int argc, _TCHAR* argv[])

SAOOD HANIF
BS(IT)
BIIT
int x,p,res=1,k=0;

cout<<"Enter a number: ";

cin>>x;

if(x>0)

for(int i=1;res<=x;i++)

res=res*2;

k++;

if(res*2>x)

break;

cout<<"2 raise to the power "<<k<<" is: "<<res<<" which is less than or equal to: "<<x<<endl;

else

cout<<"Invalid Number"<<endl;

return 0;

Program#59:
A program that accepts a positive integer n as input and then computes and outputs the sum
of all integers between 1 and n that is evenly divisible by 3.
Output:
Enter a number to limit the range (n): 26
Sum of 3, 6, 9, 12, 15, 18, 21, 24 is 108
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,res=0;

SAOOD HANIF
BS(IT)
BIIT
cout<<"Enter a number to limit the range (n): ";

cin>>x;

if(x>0)

cout<<"Sum of ";

for(int i=1;i<=x;i++)

if(i%3==0)

cout<<i<<" ";

res=res+i;

cout<<" is= "<<res<<endl;

else

cout<<"Invalid Number"<<endl;

return 0;

Program#60:
A program that determines the number of digits in a given integer by using while loop.
Output:
Enter a number: 3401198
There are 7 digits in number 3401198
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,p,res=0;

cout<<"Enter a number : ";

SAOOD HANIF
BS(IT)
BIIT
cin>>x;

p=x;

cout<<"There are ";

while(p!=0)

p=p/10;

res++;

cout<<res<<" digits in numbers "<<x<<endl;

return 0;

Program#61:
A program that computes the sum of all integers between first and second, where first and
second are integers and first<=second. Program should prompts the user to enter new values
for first and second if first>second.
Output:
Enter value for first: 26
Enter value for second: 12
Enter value for first: 5
Enter value for second: 13
Sum of 5, 6, 7, 8, 9, 10, 11, 12, 13 is 81
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,y,res=0;

jump:

cout<<"Enter value for first : ";

cin>>x;

cout<<"Enter value for second : ";

cin>>y;

if(x>y)

SAOOD HANIF
BS(IT)
BIIT
goto jump;

cout<<"Sum of ";

for(int i=x;i<=y;i++)

cout<<i<<" ";

res=res+i;

cout<<" is= "<<res<<endl;

return 0;

Program#62:
A program that takes input of a number from user and checks and prints whether is it
divisible by 7 or not. Number should be positive if user enters a negative number then
program should prompt to enter the number again.
Output:
Enter a positive number:– 20
Enter a positive number:– 6
Enter a positive number:38
38 is not divisible by 7
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x;

jump:

cout<<"Enter a positive number : ";

cin>>x;

if(x<7)

goto jump;

if(x%7==0)

cout<<x<<" is divisible by 7"<<endl;

SAOOD HANIF
BS(IT)
BIIT
else

cout<<x<<" is not divisible by 7"<<endl;

return 0;

Program#63:
A program that takes input of a number then checks whether is it prime or not and prints a
message accordingly.
Output:
Enter a number: 34
34is not a prime number
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,res=0;

cout<<"Enter a number : ";

cin>>x;

for(int i=1;i<=x;i++)

if (x%i==0)

res++;

if(res==2)

cout<<x<<" is prime a number"<<endl;

else

cout<<x<<" is not a prime number"<<endl;

return 0;

Program#64:

SAOOD HANIF
BS(IT)
BIIT
A program that takes input of integers repeatedly until user enters a negative number then
prints the largest and second largest number among them.
Output:
Enter a number: 13
Enter a number: 23
Enter a number: 51
Enter a number: 9
Enter a number: 2
Enter a number: 76
Enter a number: -9
Largest number is 76
Second largest number is 51
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,smax=0,max=0;

do{

cout<<"Enter a number : ";

cin>>x;

if(max<x)

smax=max;

max=x;

}while(x>0);

cout<<"Largest Number is "<<max<<endl;

cout<<"Second Largest Number is "<<smax<<endl;

return 0;

SAOOD HANIF
BS(IT)
BIIT
Program#65:
A program that takes input of integers repeatedly until user enters a negative number then
prints the smallest and second smallest number among them.
Output:
Enter a number: 13
Enter a number: 23
Enter a number: 51
Enter a number: 9
Enter a number: 2
Enter a number: 76
Enter a number: -9
Smallest number is 2
Second smallest number is 9

Program#66:
A program that takes input of integers repeatedly until user enters a zero digit then it prints
the number of positive and negative integers.
Output:
Enter a number: 13
Enter a number: -21
Enter a number: -51
Enter a number: -69
Enter a number: 22
Enter a number: -76
Enter a number: -9
Enter a number: 0
Positive numbers are: 4
Negative numbers are: 2
Solution:
int _tmain(int argc, _TCHAR* argv[])

int x,pos=0,neg=0;

do{

cout<<"Enter a number : ";

cin>>x;

if(x>0)

pos++;

if(x<0)

neg++;

SAOOD HANIF
BS(IT)
BIIT
}while(x!=0);

cout<<"Positive Numbers are "<<pos<<endl;

cout<<"Negitive Numbers are "<<neg<<endl;

return 0;

Shapes drawing using nested loops


Program#67:
A program that prints the following pattern and takes input of number of rows and fill
character from user. (Number of rows should be even).
Output:
Enter number of rows: 8
Enter a character: $
$$$$
$$$
$$
$
$
$$
$$$
$$$$

Program#68:
A program that prints the following pattern and takes input of number of rows and fill
character from user. (Number of rows should be even). For example
Enter number of rows: 8
Enter a character: $
$$$$
$$$
$$
$
$
$$
$$$
$$$$

Program#69:

SAOOD HANIF
BS(IT)
BIIT
A program takes a number from user then prints an equilateral triangle. For example if user
enters 5 triangle shape should be as follows
*
***
*****
*******
*********
Solution:
{
int x=1,y=1;
cout<<"Enter a number: ";
cin>>x;
int n=x;
for (int i=1;i<=x;i++)
{
for (int j=1;j<n;j++)
cout<<" ";
for(int k=1;k<=y;k++)
cout<<"*";
n--;
y=y+2;
cout<<endl;
}
return 0;
}
Program#70:
A program that prints a diamond shape dependent on user input for example if user enters 7
and a filling character @ the shape should be as follows
@
@@@
@@@@@
@@@@@@@
@@@@@
@@@
@

Program#71:
A program that reads height and width of a rectangle then prints a solid shape rectangle with
that height and width. For example output has height 3 and width 7
*******
*******
*******
Solution:
int _tmain(int argc, _TCHAR* argv[])
{
int h,w;
cout<<"Enter height: ";
cin>>h;
cout<<"Enter Width: ";
cin>>w;
for (int i=1;i<=h;i++)
{

SAOOD HANIF
BS(IT)
BIIT
for (int j=1;j<=w;j++)
cout<<"*";
cout<<endl;
}
return 0;
}
Program#72:
A program that reads a height and width of a rectangle and prints a hollow shape rectangle
that looks like the one below. This example has a height of 5 and width of 7
********
* *
* *
* *
********

Program#73:
A program that prints the following
5 1
45 21
345 321
2345 4321
12345 54321
2345 4321
345 321
45 21
5 1
Solution Left:
int_tmain(intargc, _TCHAR* argv[])
{
int x=5,y=1,n=2;
for (int i=1;i<=5;i++)
{
for(int k=1;k<x;k++)
cout<<" ";
for (int j=x;j<=5;j++)
cout<<j;
x--;
cout<<endl;
}
for(int j=1;j<5;j++)
{
for(int i=1;i<=y;i++)
cout<<" ";
for(int k=n;k<=5;k++)
cout<<k;
n++;
y++;
cout<<endl;
}

return 0;

SAOOD HANIF
BS(IT)
BIIT
}
Solution Right:
int_tmain(intargc, _TCHAR* argv[])
{
int x=1,y=1,n=4,z=5;
for (int i=1;i<=5;i++)
{
for(int k=1;k<z;k++)
cout<<" ";
for (int j=x;j>0;j--)
cout<<j;
x++;
z--;
cout<<endl;
}
for(int j=1;j<5;j++)
{
for(int i=1;i<=y;i++)
cout<<" ";
for(int k=n;k>0;k--)
cout<<k;
n--;
y++;
cout<<endl;
}

Program#74:
Use nested loops to draw the following shapes
***** * * *****
**** ** ** ****
*** *** *** ***
** **** **** **
* ***** ***** *
Solution: Solution: Solution: Solution:
int_tmain(intargc, int_tmain(intargc, int_tmain(intargc, int_tmain(intargc,
_TCHAR* argv[]) _TCHAR* argv[]) _TCHAR* argv[]) _TCHAR* argv[])
{ { { {
for(int int a=5; for(int i=1; int a=0;
i=5; i>=1; i--) for(int i=1; i<=5; i++) for(int i=5;
{ i<=5; i++) { i>=1; i--)
for(int { for(int j=1; {
j=i; j>=1; j--) for(int j<=i; j++) for(int
{ k=0;k<a;k++) { k=0;k<a;k++)

cout<<"*"; cout<<" "; cout<<"*"; cout<<" ";


} for(int j=1; } for(int j=i;
j<=i; j++) j>=1; j--)
cout<<endl<<endl; { cout<<endl<<endl; {
} }
return 0; cout<<"*"; return 0; cout<<"*";
} } } }
a--; a++;

cout<<endl<<endl; cout<<endl<<endl;
} }

SAOOD HANIF
BS(IT)
BIIT
return 0;
}
12345 2 4 6 8 10 97531 54321
1234 2468 7531 5432
123 246 531 543
12 24 31 54
1 2 1 5
Solution: Solution: Solution: Solution:
int_tmain(intargc, int_tmain(intargc, int_tmain(intargc, int_tmain(intargc,
_TCHAR* argv[]) _TCHAR* argv[]) _TCHAR* argv[]) _TCHAR* argv[])
{ { { {
int N = 5; int N = 10; int N = 9; int N = 5;
int M = N; int M = N; for (int i = for (int i =
for (int i for (int i = 0; i < 5; i++) 0; i < 5; i++)
= 0; i < N; i++) 0; i < N; i++) { {
{ { for for
(int j = N; j > 0; (int j = N; j > 0; j-
for(int j = for(int j = j=j-2) -)
1; j <= M; j++) 2; j <= M; j=j+2) { {
{ {
cout << j; cout << j;
cout << j; cout << j; } }
} } N=N-2; N--;
cout cout cout cout <<
<< endl; << endl; << endl; endl;
M--; M-=2; } }
} } return 0;
return 0; return 0; }
} }
12345 5 1 5
2345 54 21 45
345 543 321 345
45 5432 4321 2345
5 54321 54321 12345
Solution: Solution: Solution: Solution:
int_tmain(intargc, int_tmain(intargc, int_tmain(intargc, int_tmain(intargc,
_TCHAR* argv[]) _TCHAR* argv[]) _TCHAR* argv[]) _TCHAR* argv[])
{ { { {
for (int i int N = 5; int N = 5; int n=5;
= 1; i <= 5; i++) for (int i = for (int i = 0; i < for (int i =
{ 0; i < 5; i++) N; i++) 0; i<5; i++)
for { { {
(int j = i; j <= for for (int j = i+1; j for
5; j++) (int j = 5; j >= N; > 0; j--) (int j = n; j <= 5;
{ j--) { j++)
{ cout << {
cout << j; j;
} cout << j; } cout << j;
cout } cout << }
<< endl; N--; endl; n--;
} cout } cout <<
return 0; << endl; return 0; endl;
} } } }
return 0; return 0;
} }

SAOOD HANIF
BS(IT)
BIIT
HIJKLM HIJKLM ABCDEFG ZYXWVU
IJKLM IJKLMN ABCDEF ZYXWV
JKLM JKLMNO ABCDE ZYXW
KLM KLMNOP ABCD ZYX
LM LMNOPQ ABC ZY
M MNOPQR AB Z
Solution: Solution: A Solution:
int_tmain(intargc, int_tmain(intargc, Solution: int _tmain(int argc,
_TCHAR* argv[]) _TCHAR* argv[]) int_tmain(intargc, _TCHAR* argv[])
{ { _TCHAR* argv[]) {
char ch; char ch; { char ch;
int n=72; int char ch; int n=90,m=85;
for (int i n=72,m=77; int for (int i =
= 0; i<=5; i++) for (int i = n=65,m=71; 0; i<=5; i++)
{ 0; i<=5; i++) for (int i = {
for { 0; i<=6; i++) for
(int j=n; j <= 77; for { (int j=n; j>= m; j--)
j++) (int j=n; j <= m; for {
{ j++) (int j=n; j <= m;
{ j++) ch=j;
ch=j; {
ch=j; cout << ch;
cout << ch; ch=j; }
} cout << ch; m++;
n++; } cout << ch; cout <<
cout n++; } endl;
<< endl; m++; m--; }
} cout cout return 0;
return 0; << endl; << endl; }
} } }
return 0; return 0;
} }
1 2 3 4 5 0 1 2 3 4 2 4 6 8 10 2 4 6 8 10
1 3 5 7 9 1 2 3 4 5 3 6 9 12 15 4 6 8 10 12
1 4 7 10 13 2 3 4 5 6 4 8 12 16 20 6 8 10 12 14
1 5 9 13 17 3 4 5 6 7 5 10 15 20 25 8 10 12 14 16
Solution: Solution: Solution: Solution:
int _tmain(int int _tmain(int int _tmain(int argc, int _tmain(int argc,
argc, _TCHAR* argc, _TCHAR* _TCHAR* argv[]) _TCHAR* argv[])
argv[]) argv[]) { {
{ { int int n=2,m=10;
int int n=0,m=4; n=2,m=10,x=2; for (int
n=1,m=5; for (int for (int i=1;i<5;i++)
for (int i=1;i<5;i++) i=1;i<5;i++) {
i=1;i<5;i++) { { for
{ for for (int
for (int (int j=n;j<=m;j=j=j+2)
(int j=n;j<=m;j=j++) j=n;j<=m;j=j=j+x) {
j=1;j<=m;j=j+n) { {
{ cout<<j;
cout<<j; cout<<j; }
cout<<j; } } m=m+2;
} m=m+5; n=n+2;
m=m++; n++;
m=m+4; n++; x++; cout<<endl;
n++; }

SAOOD HANIF
BS(IT)
BIIT
cout<<endl; cout<<endl; return 0;
cout<<endl; } } }
} return 0; return 0;
return 0; } }
}
0 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0 1 0 0 0 1
0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0
0 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 0 1 0
1 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 1 0 0 0 1
Solution: Solution: Solution: Solution:
int _tmain(int int _tmain(int int _tmain(int argc, int _tmain(int argc,
argc, _TCHAR* argc, _TCHAR* _TCHAR* argv[]) _TCHAR* argv[])
argv[]) argv[]) { {
{ { int n=1,m=6; int x=1,y=5;
int int n=1; for (int for (int
n=1,m=5; for (int i=1;i<=5;i++) i=1;i<=5;i++)
for (int i=1;i<=5;i++) { {
i=1;i<=5;i++) { for for
{ for (int j=n;j<=m;j++) (int j=1;j<=5;j++)
for (int j=1;j<=5;j++) { {
(int {
j=n;j<=m;j=j=j++) if(j%2!=0) if(j==x||j==y)
{ if(j==n)
cout<<"1";
if(j==5) cout<<"1";
cout<<"1"; else
else
cout<<"1"; else cout<<"0";
}
else cout<<"0"; x++;
cout<<"0"; } y--;
} m++;
cout<<"0"; n++; n++; cout<<endl;
} }
m++; cout<<endl; cout<<endl; return 0;
n++; } } }
return 0; return 0;
cout<<endl; } }
}
return 0;
}

One-Dimensional Array
Program#75:
Write a program that declares an array abc of 10 elements of type int. Initialize each element
of array with cube of the index variable. Then display all values in the array using loop.
Output:
Values in array are 0 1 8 27 64 125 216 343 512 891
Solution:
int abc[10],i;
for(i=0;i<=9;i++)

SAOOD HANIF
BS(IT)
BIIT
{
cout<<"value in array is: ";
abc[i+1]=i*i*i;
cout<<abc[i+1]<<endl;
}
return 0;
}
Program#76:
A program that reads two arrays, A and B, each of same size then adds their corresponding
index elements and stores their result in a third array called S.
Output:
Enter values in Array A: 12 3 7 11 6 19 5 22 28 15
Enter values in Array B: 17 5 6 22 18 9 11 7 13 31
Sum of A and B array is: 29 8 13 33 24 28 16 29 41 46
Solution:
int arr1[10],arr2[10];
cout<<"enter the velues in array A: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
cout<<" enetr the velues in array B: ";
for(int i=0;i<10;i++)
{
cin>>arr2[i];
}
cout<<"the sum of array A and B: ";
for(int i=0;i<10;i++)
{
cout<<arr1[i]+arr2[i]<<"\t";
}
return 0;
}

Program#77:
You have two arrays A and B, each of 10 integers. Then check whether every element of
array A is equal to its corresponding element in array B. in other words, the program must
check if A[0], is equal to B[0], A[1], is equal to B[1] and so forth. Print whether both arrays
are equal or not.
Output:
Enter values in 1st array: 21 4 54 61 78 29 30 31 6 52
Enter values in2nd array: 21 4 54 61 78 29 30 31 6 52
Both Arrays are equal
Solution:
int arr1[10],arr2[10],x=0;
cout<<"enter values in 1st array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
cout<<"enter values in 2st array: ";
for(int i=0;i<10;i++)
{
cin>>arr2[i];

SAOOD HANIF
BS(IT)
BIIT
}
for(int i=0;i<10;i++)
{
if(arr1[i]==arr2[i])
x=i;
}
if(x==9)
cout<<"arrays are equal";
else
cout<<"arrays are not equal";

return 0;
}

Program#78:
Write a program that declares an integer array of size 10 and reads values from user in it.
Thenfind out how many elements are even and how many are odd.
Output:
Enter values in array: 4 2 12 65 71 34 18 98 30 12
Even numbers are: 7
Odd Numbers are: 3
Solution:
int arr1[10],x=0,y=0;
cout<<"enter values of array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
for(int i=0;i<10;i++)
{
if(arr1[i]%2==0)
x++;
else
y++;
}
cout<<"even numbers are: "<<x<<"\nodd numbers are: "<<y<<endl;

return 0;
}
Program#79:
Write a program that declares an integer array of size 10 and reads values from user in it.
Thenreplace its negative elements with same positive values.
Output:
Enter values in array A: 14 -21 12 - 65 -87 34 -58 0 30 12
After replacement values are:14 21 12 65 87 34 58 0 30 12
Solution:
int arr1[10];
cout<<"enter values of array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}

SAOOD HANIF
BS(IT)
BIIT
for(int i=0;i<10;i++)
{
if(arr1[i]<0)
arr1[i]=arr1[i]*(-1);
}
cout<<"after replacement values are: ";
for(int i=0;i<10;i++)
{
cout<<arr1[i]<<"\t";
}

return 0;
}

Program#80:
Write a program that declares an integer array of size 10 and reads values from user in it.
Calculate the average of 10 values stored in array and print all those values which are greater
than the calculated average
Output:
Values in array are 10 51 82 27 45 25 92 15 12 65
Average is= 39.9
Values greater than average are: 51 82 45 92 65
Solution:
int arr1[10],avg=0;
cout<<"enter values of array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
avg=arr1[i]+avg;
}
avg=avg/10;
cout<<"Average is ="<<avg<<endl;
cout<<"values greater then average are: ";
for(int i=0;i<10;i++)
{
if(arr1[i]>avg)
cout<<arr1[i]<<"\t";

return 0;
}

Program#81:
Write a program to copy the contents of one array into another array in reverse order. (It
means that last element of array becomes the first element and second last element becomes
second and so on)
Output:
Enter values in array A: 21 4 54 61 78 29 30 31 6 52
Values inarray B are:52 6 31 30 29 78 61 54 4 21
Solution:

SAOOD HANIF
BS(IT)
BIIT
nt arr1[10],arr2[10];
cout<<"enter values in array A: ";
for(int i=0;i<=9;i++)
{
cin>>arr1[i];
arr2[9-i]=arr1[i];
}
cout<<"values in array B: ";
for(int i=0;i<=9;i++)
cout<<arr2[i]<<"\t";
return 0;
}

Program#82:
A program that reads an array of 10 elements then calculates their squares and stores result in
another array.
Output:
Enter values in Array A: 8 3 7 11 6 12 5 22 28 15
Squares of array A values are: 64 9 49 121 36 144 25 484 784 225
Solution:
int arr1[10],arr2[10];
cout<<"enter values in array A: ";
for(int i=0;i<=9;i++)
{
cin>>arr1[i];
arr2[i]=arr1[i]*arr1[i];
}
cout<<"squares of array A values are: ";
for(int i=0;i<=9;i++)
cout<<arr2[i]<<"\t";
return 0;
}

Program#83:
A program that reads an array of 9 elements then calculates their factorials and stores result in
another array.
Output:
Enter values in Array A:8 3 7 6 2 11 5 0 9
Factorials of array A values are: 40320 6 5040 720 2 39916800 120 1 362880
Solution:
int arr1[9],arr2[9],x=0;
cout<<"enter values in array A: ";
for(int i=0;i<9;i++)
{
cin>>arr1[i];
}
for(int i=0;i<9;i++)
{
x=arr1[i];
for(int j=arr1[i];j>1;j--)
{

x=x*(j-1);
}
if(x==0)

SAOOD HANIF
BS(IT)
BIIT
arr2[i]=1;
else
arr2[i]=x;
}
cout<<"factorials of Array A values are: ";
for(int i=0;i<9;i++)
cout<<arr2[i]<<"\t";

return 0;
}

Program#84:
Write a program that inputs an integer array A of size 6 and an array B of size 7. Declare
another array of size 13 and store all values of A and B in array C.
Output:
Enter values in array A: 1 4 15 61 72 19
Enter values inarray B: 8 14 54 31 18 29 34
Values stored in C are: 1 4 15 61 72 19 8 14 54 31 18 29 34
Solution:
int arr1[6],arr2[7],arr3[13];
cout<<"enter values in array A: ";
for(int i=0;i<6;i++)
{
cin>>arr1[i];
}
cout<<"enter values in array B: ";
for(int i=0;i<7;i++)
{
cin>>arr2[i];
}
for(int i=0;i<13;i++)
{

if(i<6)
{
arr3[i]=arr1[i];
}
else
{
arr3[i]=arr2[i-6];

}
}
cout<<"values stored in C are: ";
for(int i=0;i<13;i++)
cout<<arr3[i]<<"\t";

return 0;
}

Program#85:
A program that reads an array of 10 elements and a variable from user, then finds at what
index that element is present. (Searching an element from whole array)
Output:
Enter values in Array A: 17 5 6 22 18 9 11 7 13 31

SAOOD HANIF
BS(IT)
BIIT
Enter number you want to search: 22
22 is present in array A at index 3.
Solution:
int arr1[10],x,y=0;
cout<<"enter the values of array A: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
cout<<"enter number to find: ";
cin>>x;
for(int i=0;i<10;i++)
{
if(arr1[i]==x)
cout<<"the number "<<x<<" is peresent at index "<<i<<endl;
else
y++;
}
if(y==10)
cout<<"number not found"<<endl;

return 0;
}

Program#86:
A program that reads an array of 10 elements and a variable from user then finds at what
index that element is present and replace the old value at that index with new value entered
by user. (Searching an element from whole array and replacing that element with new one)
Output:
Enter values in Array A:17 5 6 22 18 9 11 7 13 31
Enter number you want to search: 22
Enter number you want to replace with: 39
22 is present in array A at index 3.
New Array is: 17 5 6 39 18 9 11 7 13 31
Solution:
int arr1[10],x,y=0,z;
cout<<"enter the values of array A: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
cout<<"enter number to find: ";
cin>>x;
cout<<"enter number you want to replace with: ";
cin>>z;
for(int i=0;i<10;i++)
{
if(arr1[i]==x)
{
cout<<"the number "<<x<<" is peresent at index "<<i<<endl;
arr1[i]=z;
}
else

SAOOD HANIF
BS(IT)
BIIT
y++;
}
if(y==10)
cout<<"number not found"<<endl;
else
{
for(int i=0;i<10;i++)
{
cout<<arr1[i]<<"\t";
}
}
cout<<endl;
return 0;
}

Program#87:
A program that reads an array of 10 elements and a variable ‘x’ from user then searches x in
the array, if the value x exists in the array then remove the first occurrence of x, shifting each
following element left and adding a zero at the end of the array.
Output:
Enter values in Array A:17 5 6 22 18 9 11 7 13 31
Enter number you want to search: 22
After shifting New Array is: 17 5 6 18 9 11 7 13 31 0
Solution:
int arr1[10],x,y=0;
cout<<"enter values in array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
cout<<"enter number you want to search: ";
cin>>x;
for(int i=0;i<10;i++)
{
if(x==arr1[i])
y=i;
}
for(int i=y;i<10;i++)
{
arr1[i]=arr1[i+1];
}
arr1[9]=0;
cout<<"after shifting new array is: ";
for(int i=0;i<10;i++)
cout<<arr1[i]<<"\t";
return 0;
}

Program#88:
Write aprogramthat takes an integerarray of size 10 from user then checks and prints whether
it is in ascending order,descending order or unordered.
Output:
Enter numbers in the array 97 45 37 35 28 24 20 14 10 8
Array is in Descending Order
Solution:

SAOOD HANIF
BS(IT)
BIIT
int arr1[10],x=0;
cout<<"enter values in array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
for(int i=0;i<10;i++)
{
if(arr1[i]<arr1[i+1])
{
x++;
}
}

if(x==9)
{
cout<<"array is in ascending order";
}

if(x==0)
{
cout<<"array is in descending order";
}

if(x!=9 && x!=0)


cout<<"array is unordered";
return 0;
}

Program#89:
Write aprogramthat takes an integerarray of size 10 from user then prints the smallest and
largest element of the array.
Output:
Enter numbers in the array47 45 37 96 28 24 82 14 70 38
Smallest element in the array is 14
Largest element in the array is 96
Solution:
int arr1[10];
cout<<"enter values in array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
int max=arr1[0];
int min=arr1[0];
for(int i=1;i<10;i++)
{
if(max<arr1[i])
{
max=arr1[i];
}
if(min>arr1[i])
{
min=arr1[i];
}
}
cout<<"smallest element in the array is: "<<min<<endl;

SAOOD HANIF
BS(IT)
BIIT
cout<<"largest element in the array is: "<<max<<endl;

return 0;
}

Program#90:
Write aprogramthat takes an integerarray of size 10 from user then shifts its all elements to
the left and its first element on the last location. (Circular shift left)
Output:
Enter numbers in the array: 47 45 37 96 28 24 82 14 70 38
After shifting left array is : 45 37 96 28 24 82 14 70 38 47
Solution:
int arr1[10],x=0;
cout<<"enter values in array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
x=arr1[0];
for(int i=0;i<10;i++)
{
arr1[i]=arr1[i+1];
}
arr1[9]=x;
cout<<"after shifting left array is: ";
for(int i=0;i<10;i++)
{
cout<<arr1[i]<<"\t";
}
cout<<endl;

return 0;
}
Program#91:
Write aprogramthat takes an integerarray of size 10 from user then shifts its all elements to
the left and its first element on the last location. (Circular Shift Right)
Output:
Enter numbers in the array: 47 45 37 96 28 24 82 14 70 38
After shifting right array is :38 47 45 37 96 28 24 82 14 70
Solution:
int arr1[10],x=0;
cout<<"enter values in array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
x=arr1[9];
for(int i=9;i>=0;i--)
{
arr1[i]=arr1[i-1];
}
arr1[0]=x;
cout<<"after shifting right array is: ";
for(int i=0;i<10;i++)

SAOOD HANIF
BS(IT)
BIIT
{
cout<<arr1[i]<<"\t";
}
cout<<endl;

return 0;
}

Program#92:
Write aprogramthat takes an integer array of size 10 from user and an integer ‘n’ as input
then shifts the whole array ‘n’ number of times to the left.
Output:
Enter numbers in the array: 47 45 37 96 28 24 82 14 70 38
How many times you want to shift? 3
After shifting right array is :96 28 24 82 14 70 38 0 0 0
Solution:
int arr1[10],x;
cout<<"enter values in array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];
}
cout<<"how many times do you want to shift: ";
cin>>x;
for(int i=0;i<x;i++)
{
for(int j=0;j<10;j++)
{
arr1[j]=arr1[j+1];
}
arr1[9]=0;
}
cout<<"after shifting left array is: ";
for(int i=0;i<10;i++)
{
cout<<arr1[i]<<"\t";
}
cout<<endl;

return 0;
}

Program#93:
A program that reads an array of size 10, then swaps its every value at an even index with
next odd index value.
Output:
Enter values in Array: 12 32 37 39 42 49 51 56 88 105
Array after swapping is: 32 12 39 37 49 42 56 51 105 88
Solution:
int arr1[10],x=0;
cout<<"enter the values of array: ";
for(int i=0;i<10;i++)
{
cin>>arr1[i];

SAOOD HANIF
BS(IT)
BIIT
}
for(int i=0;i<10;i=i+2)
{
x=arr1[i];
arr1[i]=arr1[i+1];
arr1[i+1]=x;
}
cout<<"array after swaping is: ";
for(int i=0;i<10;i++)
{
cout<<arr1[i]<<"\t";
}
cout<<endl;

return 0;
}
Program#94:
Write aprogramthat takes an integer array of size 8 from user. Make sure entries in the array
is only multiples of 5 if user enters other value that is not a multiple of 5 then it should be
entered again. After storing values print final array.
Output:
Enter values in Array 10 12 55 50 70 19 18 10 20 80 11 13 65
Stored values in Array are 10 55 50 70 10 20 80 65
Solution:
int arr1[10],x,y=0;
cout<<"enter values in array: ";
while(y<8)
{
cin>>x;
if(x%5==0)
{
arr1[y]=x;
y++;
}
}
cout<<"stored values in array are: ";
for(int i=0;i<8;i++)
{
cout<<arr1[i]<<"\t";
}
cout<<endl;

return 0;
}

Program#95:
Write aprogramthat takes an integer array of size 8 from user and the draw a chart for each of
its input value. (Histogram)
Output:
Enter numbers in the array: 7 5 0912 4 18713 5
7 *******
5 *****
0
12 ************

SAOOD HANIF
BS(IT)
BIIT
4 ****
18 ******************
7 *******
13 *************
5 *****
Solution:
int arr1[8];
cout<<"enter the values of array: ";
for(int i=0;i<8;i++)
{
cin>>arr1[i];
}
for(int i=0;i<8;i++)
{
cout<<arr1[i]<<"\t";
for(int j=0;j<arr1[i];j++)
{
cout<<"*";
}
cout<<endl;
}

return 0;
}

Program#96:
A program that reads an array of 20 elements (in the range of 0 to 9) then calculates how
many times a digit is used in the array (calculating frequency of each digit).
Output:
Enter values in array:
2 1 8 9 0 6 4 2 5 5 7 8 9 1 9 4 9 5 2 8
0 is used 1 times
1 is used 2 times
2 is used 3 times
3 is used 0 times
4 is used 2 times
5 is used 3 times
6 is used 1 times
7 is used 1 times
8 is used 3 times
9 is used 4 times
Solution:
int arr1[20],x=0;
cout<<"enter the values of array: ";
for(int i=0;i<20;i++)
{
cin>>arr1[i];
}
for(int i=0;i<=9;i++)
{
for(int j=0;j<20;j++)
{

SAOOD HANIF
BS(IT)
BIIT
if(arr1[j]==i)
{
x++;
}
}
cout<<i<<" is used "<<x<<" times "<<endl;
x=0;
}

return 0;
}

Strings
Program#97:
Write aprogramthat takes a string as input from user then converts its capital letters into small
letters and small letters into capital letters.
Output:
Enter a string
Learn from Yesterday, Live for Today, Hope for Tomorrow
lEARN FROMyESTERDAY, lIVE FOR tODAY, hOPE FORtOMORROW

Program#98:
Write aprogramthat takes a string as input from user then prints whether it is palindrome or
not. (Palindrome is a type of word play in which a word, phrase, or sentence reads the
samebackward or forward)
Output:
Enter a string
no evil shahs live on
The given string is a Palindrome string

Program#99:
Write aprogramthat takes a string as input from user then capitalize its each word’s starting
letter.
Output:
Enter a string
We must accept finite disappointment, but never lose infinite hope
We Must Accept Finite Disappointment, But Never Lose Infinite Hope

Program#100:
Write a program that inputs a string, a sub-string and location (where user wants to add
substring) and then inserts the substring to the location provided by the user.
Output:
Enter a string:
Pleases this application and give me gratuity
Enter Substring: read

SAOOD HANIF
BS(IT)
BIIT
Location : 7
After inserting substring final output is
Pleases read this application and give me gratuity

Program#101:
Write aprogramthat takes two strings as input from user then prints whether both strings are
equal or not without using built-in function of strcmp().
Output:
Enter first string:
He that lives upon hope will die fasting
Enter second string:
He that lives upon hope will die fasting
The given strings are equal

Program#102:
Write aprogramthat takes a string and a character from user as input then remove that
character from the string and print the final string.
Output:
Enter the string:
He that lives upon hope will die fasting
Enter the character you want to remove: i
Final string after removing i is:
He that lves upon hope wll de fastng

Program#103:
Write aprogramthat takes a string as input then replaces all words of “is” by “was”.
Output:
Enter the string:
This blue pen is for the kid, which is wearing blue shirt and is looking healthy.
Final string after replacement is:
This blue pen was for the kid, which was wearing blue shirt and was looking healthy.

Two-Dimensional Array
Program#104:
A program that reads two 2-d arrays as 3*3 matrices then calculates and stores their sum into
another 2-d array of size 3*3.
Array A
3 5 6
11 9 2
23 10 7
Array B
4 6 21
12 11 9
3 9 6

SAOOD HANIF
BS(IT)
BIIT
Sum of Array A and B is
7 11 27
23 20 11
26 19 13

Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int arra[3][3],arrb[3][3],arrc[3][3];
cout<<"Enter Value in Array A: "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cin>>arra[i][j];
}
cout<<"Enter Value in Array B: "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cin>>arrb[i][j];
}
cout<<"Sum of Array A and Array B is: "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<arra[i][j]+arrb[i][j]<<"\t";
cout<<endl;
}
return 0;
}
Program#105:
A program that reads two 2-d arrays as 3*3 matrices then calculates and stores their product
into another 2-d array of size 3*3.
Array A
3 5 6
1 9 2
12 10 7
Array B
4 6 7
2 3 5
3 8 6
Product of Array A and B is
30 81 82
28 49 64
89 158 176

Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int arra[3][3],arrb[3][3],arrc[3][3];
cout<<"Enter Value in Array A: "<<endl;
for(int i=0;i<3;i++)
{

SAOOD HANIF
BS(IT)
BIIT
for(int j=0;j<3;j++)
cin>>arra[i][j];
}
cout<<"Enter Value in Array B: "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cin>>arrb[i][j];
}
cout<<"Product of Array A and Array B is: "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<arra[i][j]*arrb[i][j]<<"\t";
cout<<endl;
}
return 0;
}
Program#106:
A program that fills the right-to-left diagonal of a square matrix with 0s, the lower right
triangle with -1s, and the upper left triangle with +1s. The output of the program, assuming a
6*6 matrix, is as follows.

+1 +1 +1 +1 +1 0
+1 +1 +1 +1 0 -1
+1 +1 +1 0 -1 -1
+1 +1 0 -1 -1 -1
+1 0 -1 -1 -1 -1
0 -1 -1 -1 -1 -1
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int arr[6][6],b=6;
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
arr[i][j]=j+1;
if(arr[i][j]==b)
cout<<" 0\t";
if(arr[i][j]<b)
cout<<"+1\t";
if(arr[i][j]>b)
cout<<"-1\t";
}
cout<<endl;
b--;
}
return 0;
}
Program#107:
A program that takes a 6*6 matrix, then stores the sum of row number and column number
for each element. Output would be as follows.
1 2 3 4 5 6

SAOOD HANIF
BS(IT)
BIIT
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11
6
7 8 9 10 11 12
Solution:
int _tmain(int argc, _TCHAR* argv[])
{
int arr[6][6];
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
arr[i][j]=(j+1)+(i+1);
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}
Program#108:
A program that creates a 2-d Array matrix, representing the Pascal triangle.In a Pascal
triangle , each element is the sum of the element directly above it and the element to the left
of the element directly above it (if any). A Pascal triangle of size 7 is as follows.

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

Program#109:
A program that reads a 2-d array as 3*3 matrix then takes its transpose and stores result into
another 3*3 matrix. (Transpose of a matrix means interchanging rows with columns and
columns with rows)
Array A
3 5 6
1 9 2
12 10 7
Transpose of matrix A is
3 1 12
5 9 10
6 2 7

SAOOD HANIF
BS(IT)
BIIT
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int arr[3][3];
cout<<"Enter Array A: ";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
cout<<"Transpose of Array A is: "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<arr[j][i]<<"\t";
}
cout<<endl;
}
Program#110:
A program that reads a 2-d array as 3*3 matrix then calculates and prints its Determinant.
|A| = a11 (a22 a33 – a23 a32) – (a12 (a21 a33 – a23 a31) +a13 (a21 a32 – a22 a31)
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int arr[3][3],a;
cout<<"Enter Array A: ";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
a=arr[0][0]*((arr[1][1]*arr[2][2])-(arr[1][2]*arr[2][1]))-
arr[0][1]*((arr[1][0]*arr[2][2])-
(arr[1][2]*arr[2][0]))+arr[0][2]*((arr[1][0]*arr[2][1])-(arr[1][1]*arr[2][0]));
if(a<0)
a=a*(-1);
cout<<"Determinent of Array A is: "<<a<<endl;

return 0;
}
Program#111:
A program that reads a 2-d array as 3*3 matrix then checks whether the matrix is symmetric
or not. A matrix is symmetric if
mat [ i ] [ j ] = mat [ j ] [ i ] for all i and j except when i = j.
3 1 12
1 9 10
12 10 7
Matrix is symmetric
Solution:

SAOOD HANIF
BS(IT)
BIIT
int_tmain(intargc, _TCHAR* argv[])
{
int arr[3][3],a;
cout<<"Enter Array A: ";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(arr[i][j]==arr[j][i])
a=1;
else
goto jump;
}
}

if(a==1)
cout<<"Matrix is symmetric"<<endl;
else
{
jump:
cout<<"Matrix is not symmetric"<<endl;
}

return 0;
}
Program#112:
A program that reads a 2-d array as 3*3 matrix then checks whether the matrix is an identity
matrix or not. An identity matrix has all the entries 0 except diagonal and all entries of
diagonal are 1s.
1 0 0
0 1 0
0 0 1
Matrix is an identity matrix

Solution:
int _tmain(int argc, _TCHAR* argv[])

{
int arr[3][3],a;
cout<<"Enter Array A: ";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>arr[i][j];
}
}

SAOOD HANIF
BS(IT)
BIIT
if(arr[0][0]==1&&arr[1][1]==1&&arr[2][2]==1)
cout<<"Matrix is Identity Matrix"<<endl;
else
cout<<"Matrix is not a Identitiy Matrix"<<endl;

return 0;
}

Functions
Value returning Functions
Program#113:
Write a function that takes two values as parameter. It returns sum of x and y if x is greater
than y, otherwise it returns x minus two times y.
Solution:
int sum(intx,inty)
{
int z=0;
if(x>y)
{ z=x+y;
}
else
{
z=x-(2*y);
}
return z;
}

int_tmain(intargc, _TCHAR* argv[])


{
int a,b,c=0;
cout<<"enter 1st number =";
cin>>a;
cout<<"enter 2nd number =";
cin>>b;
c=sum(a,b);
cout<<c<<endl;
return 0;
}
Program#114:
Write a function Dconversion() that converts a 4-bit binary integer into decimal equivalent.
Function takes one parameter of binary number and returns decimal number.
Hint: The decimal equivalent of binary 1101 is 1*1+0*2+1*4+1*8 or 1+0+4+8=13
Solution:
longint Dconversion(longint bin)
{
longint deci=0,j=1,r=0;

while (bin!=0)
{

SAOOD HANIF
BS(IT)
BIIT
r=bin%10;
deci=deci+r*j;
j=j*2;bin=bin/10;
cout<<"\a";
}
return deci;
}
int _tmain(int argc, _TCHAR* argv[])
{
longint x,y=0;
cout<<"enter a binary number: ";
cin>>x;
y= Dconversion(x);
cout<<"number in decimal: "<<y<<endl;
return 0;
}

Program#115:
Write a program that by defining a function even_odd(), to test whether a given integer is
even or odd. Pass an integer value as an argument to a function. The function returns 1 if
number is Even and 0 otherwise. Print corresponding messages in main() function.
Solution:
int even_odd(inta )
{
if(a%2==0)
return 1;
else
return 0;
}

int_tmain(intargc, _TCHAR* argv[])


{
int x,y;
cout<<"enter number = ";
cin>>x;
y=even_odd(x);
if(y==1)
cout<<"the number is even"<<endl;
else
cout<<"the number is odd"<<endl;

return 0;
}
Program#116:
Write a value returning function, isVowel(), which returns the value 1 if a given character is a
vowel and otherwise returns 0.
Solution:
char Vowel(chara )
{
if(a=='a' ||a=='e'||a=='i'||a=='o' ||a=='u' || a=='A' || a=='E' || a=='I'
||a=='o' || a=='U')
return 1;
else
return 0;
}

SAOOD HANIF
BS(IT)
BIIT
int_tmain(intargc, _TCHAR* argv[])
{
char x;
int y=0;
cout<<"enter character = ";
cin>>x;
y=Vowel(x);
cout<<y<<endl;
return 0;
}

Program#117:
Write a function lcm() that takes two parameters as input then returns their least common
multiple to the main.
Solution:
int lcm(inta,intb)
{
int c=a*b;
while(a!=b)
{
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
cout<<"\a";
}
c=c/a;
return c;
}
int_tmain(intargc, _TCHAR* argv[])
{
int x,y,z=0;
cout<<"enter first number: ";
cin>>x;
cout<<"enter second number: ";
cin>>y;
z=lcm(x,y);
cout<<"the lcm of "<<x<<" and "<<y<<" is "<<z<<endl;
return 0;
}

Program#118:
Write a function reverse() that takes an integer input as argument and returns its reverse. For
example if input number is 12345 then function would return 54321.
Solution:
int reverse(intx)
{
int rev=0;
while( x!= 0 )
{
rev = rev * 10;

SAOOD HANIF
BS(IT)
BIIT
rev = rev + (x%10);
x = x/10;
}
return rev;
}

int_tmain(intargc, _TCHAR* argv[])


{
inta,b=0;
cout<<"enter number to reverse: ";
cin>>a;
b=reverse(a);
cout<<"the reverse of "<<a<<" is "<<b<<endl;

return 0;
}

Program#119:
Write a function gcd() that takes two parameters as input then returns their greatest common
divisor to the main.
Solution:
int gcd(inta,intb)
{

while(a!=b)
{
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
cout<<"\a";
}
int c=a;
return c;
}

int_tmain(intargc, _TCHAR* argv[])


{
int x,y,z=0;
cout<<"enter first number: ";
cin>>x;
cout<<"enter second number: ";
cin>>y;
z=lcm(x,y);
cout<<"the gcd of "<<x<<" and "<<y<<" is "<<z<<endl;
return 0;
}

Program#120:

SAOOD HANIF
BS(IT)
BIIT
Write a function perfect() that takes input of an integer variable then returns 1 if it is perfect,
0 otherwise. (A number is perfect if sum of factors including 1 but not the number itself is
equal to the number)
Solution:
int perfect(intx)
{
int i=1,sum=0;
while(i<x)
{
if(x%i==0)
sum=sum+i;
i++;

}
if(sum==x)
return 1;
else
return 0;

int_tmain(intargc, _TCHAR* argv[])


{
inta,b=0;
cout<<"enter a number : ";
cin>>a;
b=perfect(a);
if(b==1)
cout<<"the number "<<a<<" is perfect"<<endl;
else
cout<<"the number "<<a<<" is not perfect"<<endl;

return 0;
}

Program#121:
Two positive integers are friendly if each one is equal to the sum of the divisors (including
one and excluding the number itself) of each other. Write a function to find weather given
numbers are friendly or not.
Example, 220 and 284 are friendly. 1+2+4+5+10+11+20+22+44+55+110=284
1+2+4+71+142=220
Solution:
int friendly(intx,inty)
{
int i=1,sum1=0,sum2=0;
while(i<x)
{
if(x%i==0)
sum1=sum1+i;
i++;

}
i=1;
while(i<y)
{

SAOOD HANIF
BS(IT)
BIIT
if(x%i==0)
sum2=sum2+i;
i++;

if(sum1==y||sum2==x)
return 1;
else
return 0;

int_tmain(intargc, _TCHAR* argv[])


{
inta,b,c=0;
cout<<"enter first number : ";
cin>>a;
cout<<"enter second number : ";
cin>>b;
c=friendly(a,b);
if(c==1)
cout<<"the numbers "<<a<<" and "<<b<<" are friendly numbers"<<endl;
else
cout<<"the numbers "<<a<<" and"<<b<<" are not friendly numbers"<<endl;

return 0;

SAOOD HANIF
BS(IT)
BIIT
void Functions
Program#122:
Write a function with name equl() that takes two integer parameters by reference. It then sets
both values with the smallest value between them. Print both values in main before and after
calling the function
Output:
Enter first number 67
Enter second number 17
Before function call values are 67 , 17
Before function call values are 17 , 17
Solution:
void equal(intx, inty)
{
if(x<y)
y=x;
else
x=y;

cout<<"after function call values are: "<<x<<" , "<<y<<endl;

int_tmain(intargc, _TCHAR* argv[])


{
int a,b;
cout<<"enter first number ";
cin>>a;
cout<<"enter second number ";
cin>>b;
cout<<"before function call values are "<<a<<" , "<<b<<endl;
equal(a,b);
return 0;
}

Program#123:
Write a function that takes two integer parameters as input and prints all tables between these
two numbers.
Output:
Enter first number 3
Enter second number 5
3*1=3
3*2=6
.
.
5*8=40
5*9=45
5*10=50
Solution:
void tables(inta,intb)
{
int c=1;

SAOOD HANIF
BS(IT)
BIIT
for(int i=a;i<=b;i++)
{
for(int j=0;j<=10;j++)
{
cout<<a<<"*"<<j<<" = "<<a*j<<endl;
}
a++;
cout<<endl;
}
}

int_tmain(intargc, _TCHAR* argv[])


{
int x,y;
cout<<"Enter first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;
tables(x,y);

Program#124:
Write a function that takes two integer parameters from user and prints all prime numbers
between them.
Output:
Enter first number 10
Enter second number 40
All prime numbers between 10 and 40 are
11,13,17,19,23,29,31,37
Solution:
void prime_values(inta,intb)
{
cout<<"All prime numbers betwwen "<<a<<" and "<<b<<" are: ";
for(int i=a;i<=b;i++)
{
if(a%2!=0)
{
cout<<a<<"\t";
}
a++;
}
cout<<endl;
}

int_tmain(intargc, _TCHAR* argv[])


{
int x,y;
cout<<"Enter first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;
prime_values(x,y);
return 0;
}
Program#125:

SAOOD HANIF
BS(IT)
BIIT
Write a function that takes three integer parameters as reference and swaps their values. Also
write main program that prints the values before and after function call. (You can’t declare a
fourth variable in the program)
Output:
Enter first number 10
Enter second number 40
Enter third number 89
Before function call x=10, y=40, z=89
After function call x=89, y=10, z=40
Solution:
void swapping(inta, intb, intc)
{
a=a+b+c;
b=a-(b+c);
c=a-(b+c);
a=a-(b+c);
cout<<"After function call: a="<<a<<", b="<<b<<", c="<<c<<endl;
}

int_tmain(intargc, _TCHAR* argv[])


{
int x,y,z;
cout<<"Enter first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;
cout<<"Enter third number: ";
cin>>z;
cout<<"Before function call a="<<x<<", b="<<y<<", c="<<z<<endl;
swapping(x,y,z);
return 0;
}
Program#126:
Write a function triangle() that takes an integer input as argument and prints its triangle. For
example if user enters 6 the function should print the following triangle shape. (triangle
would always start with letter ‘Z’).

Output:
Enter a number: 6
ZYXWVU
ZYXWV
ZYXW
ZYX
ZY
Z
Solution:
void triangle(inta)
{

char ch;
int n=90,m=90-a;
for(int i=0;i<a;i++)

SAOOD HANIF
BS(IT)
BIIT
{
for(int j=n; j> m; j--)
{
ch=j;
cout<<ch;
}
m++;
cout<<endl;
}
}

int_tmain(intargc, _TCHAR* argv[])


{
int x;
cout<<"Enter a number: ";
cin>>x;
triangle(x);
return 0;
}
Program#127:
Write a function triangle() that takes an integer input as argument and prints its triangle. For
example if user enters 9 the function should print the following triangle shape. (Make sure
that the input integer is odd if user enters an even number the input should be entered again)
Output:
Enter a number: 16
Please enter an odd number: 9
97531
7531
531
31
1
Solution:
void triangle(inta)
{
int b=a;
for(int i=b;i>0;i--)
{
for(int j=b;j>=1;j-=2)
{
cout<<j;
}
cout<<endl;
b-=2;
}
}

int_tmain(intargc, _TCHAR* argv[])


{
int x;
cout<<"Enter a number: ";
cin>>x;
ac:
if(x%2==0)
{
cout<<"Please enter an odd number: ";
cin>>x;

SAOOD HANIF
BS(IT)
BIIT
goto ac;
}
else
triangle(x);
return 0;
}
Program#128:
Write a program that takes 8 inputs from user in main using loop. Each time an input is given
it is passed to a function Slarge(). Slarge function takes one input as parameter and keeps
record of largest number and second largest number entered till now. (Hint: use global
variables to keep record of largest and second largest numbers)
Output:
Enter numbers: 16
5
2
11
90
16
73
65
Largest number is 90 and second largest is 73

Passing Arrays to Functions


Program#129:
Write a program that inputs two integer arrays from user of size 8 then passes these two array
and another array of same size to the function small(). The function small() then compares the
input integer array values and stores the smallest of them in third array. For example it first
compares a[0] and b[0] the smallest value between them is stored in c[0].
Output:
Enter numbers in array A: 6 12 87 19 19 10 20 91
Enter numbers in array B: 7 8 15 52 11 4 40 100
After function call array C is:6 8 15 19 11 4 20 91
Solution:
void small(intarr[],intarr2[])
{
int arr3[8];
for(int i=0;i<8;i++)
{
if(arr[i]>=arr2[i])
arr3[i]=arr2[i];
else
arr3[i]=arr[i];
}
cout<<"After function call: ";
for(int i=0;i<8;i++)
cout<<arr3[i]<<"\t";
}

int main()

SAOOD HANIF
BS(IT)
BIIT
{
int arr[8],arr2[8];
cout<<"Enter values of array: ";
for(int i=0;i<8;i++)
{
cin>>arr[i];
}
cout<<"Enter values of array: ";
for(int i=0;i<8;i++)
{
cin>>arr2[i];
}
small(arr,arr2);
return 0;
}
Program#130:
Write a program that inputs a string of size 100 and an integer from user then passes both to a
function as arguments. The function then extract given number of characters from the string.
Output:
Enter a string
The best way to find yourself is to lose yourself in the service of others
Enter an integer:17
Extracted string is:
service of others

Program#131:
Write a function that takes an integer array and its size as arguments and then shift the
negative numbers to the left and positive numbers to the right.
Output:
Enter numbers in the array: 3 -5 1 3 7 0 -15 3 -7 -8
The array after function call is: -5 -15 -7 -8 3 1 3 7 0 3
Solution:
int b,a=1;
void neg_swap(intarr[10])
{
for(int j=0;j<10;j++)
{
for(int i=a;i<10;i++)
{
if(arr[j]>0&&arr[i]<0)
{
b=arr[j];
arr[j]=arr[i];
arr[i]=b;
a++;
break;
}
}

cout<<"After function call: ";


for(int k=0;k<10;k++)
{cout<<arr[k]<<"\t";

SAOOD HANIF
BS(IT)
BIIT
}
}

int_tmain(intargc, _TCHAR* argv[])


{
int arr[10];
cout<<"Enter number in the array: ";
for(int i=0;i<10;i++)
{
cin>>arr[i];
}

neg_swap(arr);
return 0;
}
Program#132:
Write a program that takes a string as argument and a character from user then passes both of
them to a function as arguments. If the character passed to the function is ‘g’ then it replaces
all of the ‘g’ with ‘G’ and ‘G’ with ‘g’.
Output:
Enter a string
There are two tragedies in life. One is to lose your heart's desire. The other is to gain it.
Enter a character to replace: T
there are Two Tragedies in life. One is to lose your hearT's desire. the oTher is To gain iT.

Program#133:
Write a function that takes an integer array as an argument and its size also. The function then
prints the smallest even element and smallest odd element of the arry
Output:
Enter 10 numbers in the array:
5 22 7 15 3 8 12 19 17 31
Smallest even number is 8
Smallest odd number is 3
Solution:
void even_odd(intarr[10])
{
int odd=0,even=0;
for(int i=0;i<10;i++)
{
if(arr[i]%2==0)
{
if(arr[i]<even)
even=even;
else
even=arr[i];
}
else
{
if(arr[i]<odd)
odd=odd;
else
odd=arr[i];
}

SAOOD HANIF
BS(IT)
BIIT
}
for(int i=0;i<10;i++)
{
if(arr[i]%2==0)
{
if(arr[i]<even)
even=arr[i];
else
even=even;
}
else
{
if(arr[i]<odd)
odd=arr[i];
else
odd=odd;
}

cout<<"Smallest even number is: "<<even<<endl;

cout<<"Smallest odd number is: "<<odd<<endl;


}
int_tmain(intargc, _TCHAR* argv[])
{
int arr[10];
cout<<"Enter 10 number in array: ";
for(int i=0;i<10;i++)
{
cin>>arr[i];
}
even_odd(arr);
return 0;
}
Program#134:
Write a program that takes a 2-D character array and stores 6 names in it ( an array of strings
of 6*80 size) then pass that array to a function named swap(). The swap() function will swap
first name with last name and print the array of strings.
Output:
Enter 6 names:
Abdul Manan
Ali Hamza
Bilal Ahmad
M. Salman
Zahid Khan
Rehan Ali
After function call names are
Rehan Ali
Ali Hamza
Bilal Ahmad
M. Salman
Zahid Khan
Abdul Manan

SAOOD HANIF
BS(IT)
BIIT
Program#135:
Write a function that takes an array A of 3*3 size as an argument and calculates matrix B
such that each element in matrix B is equal to sum of neighbors of the corresponding
elements in the matrix A. consider sample given below:

1 2 3 11 19 13
4 5 6 23 40 27
7 8 9 17 31 19

Note that neighbours of A[0][0] are 2, 4, and 5


Therefore B[0][0] = 2 +4 + 5 =11
Similarly, neighbours of A[1][1] are 1, 2, 3, 4, 6, 7, 8, 9
Therefore B[1][1] = 1 +2 + 3+ 4+ 6+ 7+ 8+ 9 = 40

Recursion
Program#136:
Write a program that uses a recursive function to compute factorial of a number.

Program#137:
Write a program that takes two numbers from user as first and second of the Fibonacci Series
and then takes the number of the term that user wants to find in the series. You have to write
a recursive function to perform this task.

Program#138:
Write a recursive program that takes two numbers from user and calculates quotient using
recursive function without using / operator.

Program#139:
Write a recursive function that finds out whether a number is even or odd. You can’t use %
operator. If you subtract 2 repeatedly from a number then finally the number will be reduced
to 0 or 1, that would be base case for recursive call.

Program#140:
Write a recursive function power that takes as parameter two integers x and y such that x is
nonzero and returns xy. You can use the following recursive definition to calculate xy. If
y>=0,

1 if y = 0
Power(x,y)={ x ify = 1 }
x ∗ power(x, y − 1)otherwise

SAOOD HANIF
BS(IT)
BIIT
Program#141:
Write a program that takes an integer array and passes it to a recursive function. The function
then returns the largest number among array elements.

Program#142:
The expression of computing C (n,r), the number of combinations of n items taken r at a time
is C(n,r) = n! / r! (n-r)!.. Write a recursive function to calculate the C. moreover
C (n,0) = C(n,n) = 1.
It is also known that C(n,r) = C(n – 1 , r - 1) + C(n-1 , r) if 0<k<n

Program#143:
Write recursive GCD function, that finds greatest common divisor of two integers.
Mathematical Rule is given below.
gcd (x,y) = x if y=0
gcd(y, x%y) if y≠0

Program#144:
Write a recursive function, that takes a string as argument and prints whether it is palindrome
or not.

Pointers
Note: In all the following programs the variable values should not be used directly. Use
pointer notation to manipulate and printing the values.

Program#145:
Write a program that inputs two floating point numbers from user using pointer notation. Add
two times first value into second value. Add second value three times into first value. Then
display the final values stored in variables.
Output:
Enter first number: 13.25
Enter second number: 8.61
After calculation
First number is: 30.47
Second number is: 48.36
Solution:
int _tmain(int argc, _TCHAR* argv[])
{
float x, y,z;
float *ptr=&x,*pptr=&y,*ppptr=&z;
cout<<"Enter first value: ";
cin>>*ptr;
cout<<"Enter second value: ";
cin>>*pptr;
z=x;
*ptr=*pptr*3+*ptr;
cout<<"After calculation"<<endl;
cout<<"First number is: "<<*ptr<<endl;
*pptr=*ppptr*2+*pptr;
cout<<"Second value is: "<<*pptr<<endl;

SAOOD HANIF
BS(IT)
BIIT
return 0;
}
Program#146:
Write a program that inputs two integers and then swaps the values and the program finally
displays the value using pointers.
Output:
Enter first number: 13
Enter second number: 61
After swapping
First number is: 61
Second number is: 13
Solution:
int _tmain(int argc, _TCHAR* argv[])
{
float x, y;
float *ptr=&x,*pptr=&y,*ppptr;
cout<<"Enter first value: ";
cin>>*ptr;
cout<<"Enter second value: ";
cin>>*pptr;
float z=*ptr;
*ptr=*pptr;
*pptr=z;
cout<<"After Swaping"<<endl;
cout<<"First number is: "<<*ptr<<endl;
cout<<"Second value is: "<<*pptr<<endl;

return 0;
}

Program#147:
Write a program that inputs two characters from user and change their cases. If both
characters are small alphabets then convert them into capital alphabets, if both are capital
alphabets then convert them in small alphabets, if anyone of them is small and other is capital
then change their cases.
Output:
Enter an alphabet: h
Enter an alphabet: Y
After changing case output is H and y
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
char ch,ch1;
char *ptr=&ch,*pptr=&ch1;
cout<<"Enter an alphabet: ";
cin>>*ptr;
cout<<"Enter an alphabet: ";
cin>>*pptr;
if(*ptr>=65&&*ptr<=90)
*ptr=*ptr+32;
if(*ptr>=97&&*ptr<=122)
*ptr=*ptr-32;
if(*pptr>=65&&*pptr<=90)

SAOOD HANIF
BS(IT)
BIIT
*pptr=*pptr+32;
if(*pptr>=97&&*pptr<=122)
*pptr=*pptr-32;
cout<<"After changing case Output is "<<*ptr<<" and "<<*pptr<<endl;
return 0;
}

Program#148:
Write a program that inputs three numbers from user and then prints the medium value
among the three inputs.
Output:
Enter first number: 67
Enter second number: 17
Enter third number: 34
The number with medium value is 34
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int x,y,z;
int *ptr=&x,*pptr=&y,*ppptr=&z;
cout<<"Enter first value: ";
cin>>*ptr;
cout<<"Enter second value: ";
cin>>*pptr;
cout<<"Enter third value: ";
cin>>*ppptr;
if((*ptr>=*pptr&&*ptr<=*ppptr)||(*ptr>=*ppptr&&*ptr<=*pptr))
cout<<"The number with medium value is :"<<*ptr<<endl;
if((*pptr>=*ptr&&*pptr<=*ppptr)||(*pptr>=*ppptr&&*pptr<=*ptr))
cout<<"The number with medium value is :"<<*pptr<<endl;
if((*ppptr>=*pptr&&*ppptr<=*ptr)||(*ppptr>=*ptr&&*ppptr<=*pptr))
cout<<"The number with medium value is :"<<*ppptr<<endl;
return 0;
}

Program#149:
Write a program to input a number from keyboard until user enters a zero. Every time a
number is entered the program should display whether the number is greater than, less than,
or equal to the previous number.
Output:
Enter a number: 26
Enter a number: 17
17 is smaller than 26
Enter a number: 32
32 is greater than 17
Enter a number: 32
32 is equal to 32
Enter a number: 0
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int x,y;
int *ptr=&x,*pptr=&y;

SAOOD HANIF
BS(IT)
BIIT
cout<<"Enter first value: ";
cin>>*ptr;
while(*pptr!=0)
{
cout<<"Enter second value: ";
cin>>*pptr;
if(*ptr>*pptr)
cout<<*ptr<<" is greater than "<<*pptr<<endl;
if(*ptr<*pptr)
cout<<*ptr<<" is less than "<<*pptr<<endl;
if(*ptr==*pptr)
cout<<*ptr<<" is equal than "<<*pptr<<endl;
*ptr=*pptr;
}
return 0;
}
Program#150:
Write a program that inputs five integers from user and prints their sum. The numbers should
be in the range of 20 to 50 if user enters a number out of this range then it should be entered
again.
Output:
Enter a number: 30
Enter a number: 17
17 is out of range please enter again
Enter a number: 22
Enter a number: 45
Enter a number: 59
59 is out of range please enter again
Enter a number: 41
Sum of 5 numbers=138
Solution:
int_tmain(intargc, _TCHAR* argv[])
{
int a,b=0,x=0;
int *ptr=&a;
jump:
cout<<"Enter a number: ";
cin>>*ptr;
if(*ptr>=20&&*ptr<=50)
{
x++;
b=b+*ptr;
if(x==5)
cout<<"Sum of 5 number= "<<b<<endl;
else
goto jump;
}
else
{
cout<<*ptr<<" is out of range please enter again"<<endl;
goto jump;
}

return 0;
}

SAOOD HANIF
BS(IT)
BIIT
Passing Pointers to functions
Program#151:
Write a program that inputs an integer value from user using pointer notation. Pass that value
to a function named Check that receives value in a pointer. The function then checks if the
number is even it returns a pointer that stores square of the number, and returns cube
otherwise. Display the result in main.
Output:
Enter a number: 13
Final result is: 2197

Program#152:
Write a program that inputs two integer values from user using pointer notation. Pass these
values to a function using pointers. The function then returns a pointer stored first – second if
first value is greater than second and returns first + second otherwise. Display the result in
main.
Output:
Enter first number: 13
Enter second number: 35
Final result is: 48

Program#153:
Write a program that inputs a string from user in main. Pass that string to a function using
pointer. The function then reverses the string using pointer notation and a single repetition
statement. Also display final string in main.
Output:
Enter a string
Arrays are called constant pointers
Reverse of string is
sretniop tnatsnoc dellac era syarrA

Program#154:
Write a program that inputs two integer arrays of size 8 from user using pointer notation. Pass
these arrays to a function using pointers. The function then returns 1 to main if both arrays
are reverse of each other and returns 0 otherwise. Display message in main accordingly.
Output:
Enter values in first array: 7 5 19 28 16 10 18 39
Enter values in second array: 39 18 10 16 28 19 5 7
Both arrays are reverse of each other

Program#155:
Write a program that inputs a character array of size 10 from user. Pass that array to a
function using pointer that checks whether the characters in array are entered in ascending
order that is the first letter is smaller than the next one and so on. The function returns 0 to

SAOOD HANIF
BS(IT)
BIIT
main if array is in ascending order, returns 1 if array is in descending order and 2 otherwise.
Print appropriate messages in main.
Output:
Enter characters in array: b e h i k m n q s y
Array is in Ascending order

Program#156:
Write a program that inputs a string from user in main. Pass that string to a function using
pointer. The function then removes all the vowels from string. Also display final string in
main.
Output:
Enter a string:
Only two things are infinite, the universe and human stupidity
After removing vowels string is
nly tw thngs r nfnt, th nvrs nd hmn stpdty

Program#157:
Write a program that takes six numbers from user and stores their addresses in an array of
pointer then finds and prints the smallest number using only pointer notation.
Output:
Enter first number: 30
Enter second number: 17
Enter third number: 22
Enter fourth number: 45
Enter fifth number: 41
Enter sixth number: 61
Smallest number is 17

Dynamic Memory Allocation


Program#158:
Write a program that inputs two floating point dynamic values. It then sets value to zero if a
number is greater than 50.
Output:
Enter first value:19.32
Enter second value:63.5
First value is 19.32 and second value is 0

Program#159:
Write a program that takes two one-D integer arrays and inputs their size from user.Then pass
both the arrays and their size to a function. The function would then return the largest number
among both the array elements and print it in main.
Output:
Enter size of array1:6
Enter values in array1: 4 12 41 32 16 91
Enter size of array2:8
Enter values in array2: 63 56 11 18 0 42 61 55

SAOOD HANIF
BS(IT)
BIIT
Largest element is 91

Program#160:
Write a program that inputs three dynamic integer values from user then pass these to a
function gcd(). The function then returns the greatest common divisor of these numbers. Print
the result in main.
Output:
Enter first number:20
Enter second number:45
Enter third number:30
The greatest common divisor of 20, 45 and 30 is5

Program#161:
Write a programthat takes two dynamic integer arrays storing only binary digits in it. Your
program should sum both the arrays and store result in a third array in binary form. (Binary
addition).
Output:
Enter binary digits in array1: 1 0 1 1 1 0 1
Enter binary digits in array2: 1 0 1 0 1 1 1
Sum of binary digits is : 1 0 1 1 0 1 0 0

Program#162:
Write a program that computesproduct of two matrices. Dimension of matrices must be taken
from the user. Your program must display the result in the matrix form if multiplication of
matrices is possible otherwise your program should display an error message.
Output:
Enter rows for matrix1: 2
Enter columns for matrix1:2
Enter values in matrix1:
3 5
9 8
Enter rows for matrix2: 3
Enter columns for matrix2:3
Enter values in matrix1:
11 5 21
19 2 62
8 9 7
Multiplication of matrices is not possible

Program#163:
Write a program that takes input of a dynamic variable repeatedly from user until user enters
5 odd numbers. Ignoring even numbers, your program should calculate sum of these odd
numbers.
Output:
Enter 5 0dd numbers:
6
11

SAOOD HANIF
BS(IT)
BIIT
7
13
14
21
33
Sum of odd numbers is: 85

Program#164:
Write a programthat takes one dynamic integer array storing only binary digits in it. Your
program should then print 2’s complement of this binary series.
Output
Enter binary digits in array: 1 0 1 1 1 1 0
2’s complement ofbinary is: 0 1 0 0 0 1 0

Structures
Program#165:
Creates a user defined structure type Time with three integer members hours, minutes and
seconds. The program defines a single Time structure type variable dinner_time. It uses the
dot operator to initialize the structures members with values given by user. Also write the
print function which takes a time type variable. It prints its time in hh:mm:ss format. Also
and call a function next40 which changes the given time type variable by adding 40 minutes
to it and then print it.

Program#166:
Create a user defined structure type Rectangle with these integer members, length and width.
The program defines a rectangle r1 and initializes the members according to user input. Then
write a function area which will take a rectangle type variableand calculate the area of that
rectangle call a function print in main which prints the given rectangle and also prints the area
by calling area function.

Program#167:
Write a function called increment that accepts a date structure with three fields each field is
an integer, one for the month, one for the day in the month, and one for the year. The function
increments the date by one day and returns the new date. If the date is the last day in the
month, the month field must also be changed. If the month is December, the value of the year
field must also be changed when the day is 31. A year is a leap year if it is evenly divisible by
4 but not by 100 or it is divisible by 400.

Program#168:
Write a function called futureDate. The function is to use two parameters. The first parameter
is a structure containing today’s date. The second parameter is an integer showing the number
of days after today. The function returns a structure showing the next date, which may be in a
future year.

Program#169:

SAOOD HANIF
BS(IT)
BIIT
A point in a plane can be represented by its two coordinates, x and y. therefore, we can
represent a point in a plane by a structure having two fields, as shown below.
struct POINT
{
int x;
int y;
};
Write a function that accepts the structure representing a point and returns an integer(1, 2, 3,
or 4) that indicates in which quadrant the point is located, as shown below.

Qua x Y
rtile I positive positive
2 1
coo II negative positive
3 4
rdin III negative negative
ates IV positive negative

Program#170:
A straight line is an object connecting two points. Therefore, a line can be represented by a
nested structure having two structures of type POINT, as defined in the above problem.
struct LINE
{
POINT beg;
POINT end;
};

Program#171:
Write a function that accepts two parameters of type POINT and returns a structure of type
LINE representing the line connecting the two points.

Program#172:
Write a function that accepts a structure of type LINE and returns an integer (1, 2, 3), where 1
means vertical, 2 means horizontal, and 3 means oblique. A vertical line is a linewhose x
coordinates are the same. A horizontal line is the line whose y coordinates are the same. An
oblique line is a line that is not vertical or horizontal.

Filing
Program#173:
Write a program that creates a new file and stores some text in it after writing text in the file
it also outputs file contents.

Program#174:
Write a program that opens an existing file in append mode and stores some new text in it
after writing text in the file it also outputs file contents.

Program#175:

SAOOD HANIF
BS(IT)
BIIT
Write a program that reads a file and then counts small and capital alphabets in it

Program#176:
Write a program that reads a file and then counts total number of words in it

Program#177:
Write a program that reads a file and count total number of ‘spaces’, ‘char’ and ‘digits’
comes.

Program#178:
Write a program that reads a file contents and copies them into another file.

Program#179:
Write a program that reads a file and then count how many time word ‘the’ comes in the file.

Program#180:
Write a program that reads a file and checks if there is any space, it replace that space with
TAB space.

Program#181:
Write a program to copy one file to another. While doing so replace all lowercase characters
to their equivalent uppercase characters.

Program#182:
Write a program that prints itself on the screen. It means program will read its own file and
will print data on screen.

Program#183:
Write a program that reads a file and verify that numbers of right and left braces are equal
e.g. {,}
Program#184:
Write a program to duplicate the fourth line in the file. Assume that the opened file does have
more than four lines.

Program#185:
Write a program that opens a C++ file and creates its copy by eliminating all the comments.
Note that the comments are of two types: Single line or multi line comments.

Program#186:
Write a program that opens a cpp file and finds how many integer variables are declared in it.

Program#187:
Write a program that calculates total number of characters in each line of the file.
___________________

SAOOD HANIF
BS(IT)
BIIT

You might also like