موسوعة امثلة C++ المحلولة
موسوعة امثلة C++ المحلولة
موسوعة امثلة C++ المحلولة
#include<iostream.h>
#include<conio.h>
void main(){int x=10;
int y=20;
int z;
z=x+y;
cout<<"the Result of z is:\n\a";
cout<<z;
getch();}
Q3: write a C++ program to calculate the sum and average of six students degrees and print
the appropriate result in the screen using (switch) statement:
#include<iostream.h>
#include<conio.h>
void main()
{int y;
float x1,x2,x3,x4,x5,x6,sum,av;
cout<<"Enter 6 integer number you want \n";
cin>>x1>>x2>>x3>>x4>>x5>>x6;
sum=x1+x2+x3+x4+x5+x6;
av=sum/6.0;
y=av/10;
cout<<sum<< "\\" << "\\" << "The Summation\n";
cout<<av<< "\\" << "\\" << "The Average\n";
switch (y)
{case 10,9:cout<<"Excellent\n";
break;
case 8:cout<<"Very good\n\a";
break;
case 7:cout<<"Good\n";
break;
case 6:cout<<"Medium\n";
break;
case 5:cout<<"Pass\n";
1
break;
case 4,3,2,1:cout<<"Failed\n";
break;}
getch ();}
2
Q6: a program to divide two numbers and add the result to another number and print the
result to screen:
#include<iostream.h>
#include<conio.h>
void main()
{int x=100;
int y=20;
int z=30;
int w;
w=(x/y) + z;
cout<<"the Result of z is:\n\a";
cout<<w;
getch();}
Q7: a program to calculate the mod of two numbers and output the result:
#include<iostream.h>
#include<conio.h>
void main()
{int x=105;
int y=20;
int z;
z=x%y;
cout<<"the Result of z is:\n\a";
cout<<z;
getch();}
Q9: a program to read ten numbers and calculate the sum and average of them using (for)
statement:
#include<iostream.h>
#include<conio.h>
void main() {int i,x;
float av;
int sum=0;
3
cout<<"Enter 10 Integer Number\n";
for (i=1;i<=10;i++)
{cin>>x;
sum=sum+x;}
av=sum/10;
cout<<"\n The Summation="<<sum;
cout<<"\n The Average="<<av;
getch();}
Q10: write a program in C++ to print the numbers between 1 and 50 that are dividable on 3
using (while) statement:
#include<iostream.h>
#include<conio.h>
void main()
{int j=1;
while (j<=50)
{if ((j%3)==0)
cout<<j<<"\n";
j++;}
getch();}
Q11: a program in C++ to print the numbers between 1 and 10 using (Do-While) statement:
#include<iostream.h>
#include<conio.h>
void main()
{int j=1;
do
{cout<<j<<"\n";
j++;}
while (j<=10);
getch();}
Q14: write a c++ program to read a matrix (array) with n*n and find the largest number in
the second diagonal.
#include<iostream.h>
main(){int i,j,a[3][3],max;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>a[i][j];
for (i=0;i<3;i++)
for (j=0;j<3;j++)
if (i+j==2)
{max=a[i][2-i];
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{
if (a[i][2-i]>max)
max=a[i][2-i]; }}
cout<<"maximum number in the second diagonal is"<<max; }
Q16: a program to read four degrees and calculate the sum and average of them, and print it:
#include <iostream.h>
main(){
int m1, m2, m3, m4;
cout<<"Enter marks \n";
cin>>m1>>m2>>m3>>m4;
cout<<"avg = "<< (m1+m2+m3+m4)/4;}
Q17: a program to read a Fahrenheit degree and convert it to Celsius degree using the
conversion equation: c=(f-32)*5/9
#include <iostream.h>
5
main(){ float f;
cout<<"f = ";
cin>>f;
cout<<"c = "<< (f-32)*5.0/9.0;}
Q18: write a program to read a Celsius degree and convert it to Fahrenheit degree
temperature using the equation: f=c*9/5+32:
#include <iostream.h>
main(){ float c;
cout<<"c = "; cin>>c;
cout<<"f = "<< c*9.0/5.0+32;}
4.5( x 2.3 y ) 2 5 x y
Q19: a program to calculate a and b from the equations: b وa
zw z 2.7 w
#include <iostream.h>
#include <math.h>
main(){ float w,x,y,z;
cout<<"Enter w,x,y,z\n";
cin>>w>>x>>y>>z;
cout<<"a = \n"<< ((5+x)/z) + (y/2.7*w);
cout<<"b = "<< (4.5*pow(x+2.3*y,2)) / (z+w);}
Q20: write a program to read a real number and print (positive) when it is greater than zero
and negative otherwise:
#include <iostream.h>
main(){ float n;
cout<<"n = ";
cin>>n;
if(n>=0) cout<<"Positive";
else cout<<"Negative";}
Q21: a program to read two numbers and exchange them if the first one is greater than the
second one:
#include <iostream.h>
main(){ int a, b, t;
cout<<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
if(a>b) {
t=a;
a=b;
b=t;}
6
cout<<a<<" "<<b;}
Q22: a program to read a number and print its type (even or odd):
#include <iostream.h>
main(){ int n;
cout<<"n = "; cin>>n;
if(n % 2 != 0) cout<<"Odd";
else cout<<"Even";}
Q24: a program to read two numbers and print the value of the greater one:
#include <iostream.h>
main(){ int a, b;
cout<<"a = "; cin>>a;
cout<<"b = "; cin>>b;
if(a>b) cout<<a;
else cout<<b;}
Q25: write a program to read two numbers and an operation to apply it on them:
#include <iostream.h>
main(){ float a, b;
char s;
cout<<"a, s, b \n";
cin>>a>>s>>b;
if(s=='+') cout<<a+b;
if(s=='-') cout<<a-b;
if(s=='*') cout<<a*b;
if(s=='/') cout<<a/b;}
7
#include <iostream.h>
#include <math.h>
main() { int x, y;
cout<<"x = "; cin>>x;
switch(x) {
case -5: y=3*x-7; break;
case 2:
case 5: y=5*pow(x,2); break;
case -4:
case 4: y=x-4*pow(x,3); break; }
cout<<"y = "<< y;}
Q27: write a program to calculate the factorial of a number after reading it:
#include <iostream.h>
main(){ int n, f=1;
cout<<"n = "; cin>>n;
for(int i=0; i<n; i++)
f *= (n-i);
cout<<"f = "<< f;}
Q28: write a program to calculate the summation of the numbers: 4 , 4.5 , 5 , … , 9.5 , 10
#include <iostream.h>
main(){ float n=4, sum=0;
do { sum += n;
n += 0.5;
} while(n<=10);
cout<<"sum = "<< sum;}
Q30: a program to read (n) numbers and print the maximum one among them:
#include <iostream.h>
main(){ int n, max, x;
cout<<"n = "; cin>>n;
8
cout<<"x1 = "; cin>>x;
max = x;
for(int i=2; i<=n; i++)
{cout<<"x"<<i<<" = "; cin>>x;
if(x > max) max=x;}
cout<<"max = "<< max;}
Q31: a program to read (n) numbers and print the minimum one among them:
#include <iostream.h>
main(){int n, min, x;
cout<<"n = "; cin>>n;
cout<<"x1 = "; cin>>x;
min = x;
for(int i=2; i<=n; i++)
{cout<<"x"<<i<<" = "; cin>>x;
if(x < min) min=x; }
cout<<"min = "<< min;}
Q32: write a program to calculate the distance between two points using the following
equation: d= (ax bx) 2 (ay by) 2 (az bz ) 2
#include <iostream.h>
#include <math.h>
main(){ float ax, ay, az, bx, by, bz;
cout<<"Enter ax, ay, az \n";
cin>>ax>>ay>>az;
cout<<"Enter bx, by, bz \n";
cin>>bx>>by>>bz;
cout<<"d = "<<
sqrt(pow(ax-bx,2)+pow(ay-by,2)+pow(az-bz,2));}
Q33: write a program to read a triangle three sides and print " equilateral" when all the sides
are equal and print "isosceles" when two sides are equal and print "scalene" when all the
sides are different:
#include <iostream.h>
#include <math.h>
main(){ float L1, L2, L3;
cout<<"Enter L1, L2, L3 \n";
cin>>L1>>L2>>L3;
if(L1==L2&&L1==L3&&L2==L3) cout<<"Equilateral";
else
if(L1==L2||L1==L3||L2==L3) cout<<"Isosceles";
else
9
if(L1!=L2&&L1!=L3&&L2!=L3) cout<<"Scalene";}
Q34: write a program to calculate the area of a triangle using the following equations:
Area= s(s L1)( s L2)( s L3) as L1, L2, L3 are the triangle sides and s=(L1+L2+L3)/2
#include <iostream.h>
#include <math.h>
main(){ float L1, L2, L3, s;
cout<<"Enter L1, L2, L3 \n";
cin>>L1>>L2>>L3;
s=(L1+L2+L3)/2;
cout<<"area = << sqrt(s*(s-L1)*(s-L2)*(s-L3));}
10
Q38: to show the stars in the figure below:
#include <iostream.h> // * * * *
void main() // * * *
{ // **
for(int i=4;i>=1;i--) // *
{
for(int j=1;j<=4-i;j++)
cout<<" ";
for(int k=1;k<=i;k++)
cout<<"*";
cout<<endl; }}
------------------------
Q45: write a program to solve the following series: s=( x + x*2/2 + x*3/3 + x*4/4 +....+
x*n/n)
# include <iostream.h>
void main(){ int j,x,p,n;
double i,s=0.0;
cout<<"Enter x value\t";
12
cin>>x;
cout<<"Enter n value\t";
cin>>n;
for(i=1;i<=n;i++)
{p=1;
for(j=1;j<=i;j++)
p=p*x;
s=s+p/i; }
cout<<"The sum =\t "<<s;}
-----------------------
Q46: write a program to solve the following series: s=( x*2/2! + x*4/4! + x*6/6!+....+
x*n/n!)
# include <iostream.h>
void main(){ int j,x,k,i,cp=2,cf=2,n;
double f,p,s=0.0;
cout<<"Enter x value\t";
cin>>x;
cout<<"Enter n value\t";
cin>>n;
for(i=1;i<=n;i++)
{p=1;
f=1;
for(j=1;j<=cp;j++)
p=p*x;
for(k=1;k<=cf;k++)
f=f*k;
s=s+p/f;
cp+=2;
cf+=2; }
cout<<"The sum =\t "<<s;}
----------------------
Q47: write a program to solve the following series: s=( x*1/2! + x*3/4! + x*5/6!+....+
x*a/(a+1)!)
# include <iostream.h>
void main(){ int j,x,k,i,cp=1,cf=2,n;
double f,p,s=0.0;
cout<<"Enter x value\t";
cin>>x;
cout<<"Enter n value\t";
cin>>n;
for(i=1;i<=n;i++)
{ p=1;
f=1;
for(j=1;j<=cp;j++)
p=p*x;
for(k=1;k<=cf;k++)
13
f=f*k;
s=s+p/f;
cp+=2;
cf+=2; }
cout<<"The sum =\t "<<s;}
-------------------------
Q48: write a program to solve the following series: s=( x*1/2! + x*3/4! + x*5/8! + x*7/16!
+....+ x*11/64!).
# include <iostream.h>
void main(){ int j,x,k,i,cp=1,cf=2;
double f,p,s=0.0;
cout<<"Enter x value\t";
cin>>x;
for(i=1;i<=6;i++)
{ p=1;
f=1;
for(j=1;j<=cp;j++)
p=p*x;
for(k=1;k<=cf;k++)
f=f*k;
s=s+f/p;
cp+=2;
cf*=2; }
cout<<"The sum =\t "<<s;}
----------------------------------
Q50: write a program to solve the following series: s= ( 2!/x*2 – x*4 /4! + 8!/x*8 – x*16
/16! +....- x*64/64!)
# include <iostream.h>
void main(){ int j,x,k,i,cp=2,cf=2,c=1,m=1;
double f,p,s=0.0;
cout<<"Enter x value\t";
cin>>x;
for(i=1;i<=6;i++)
{ p=1;
f=1;
for(j=1;j<=cp;j++)
p=p*x;
for(k=1;k<=cf;k++)
f=f*k;
if(c%2==1)
s=s+m*f/p;
else
s=s+m*p/f;
cp*=2;
cf*=2;
m*=-1;
14
c++; }
cout<<"The sum =\t "<<s;}
Q51: write a program to solve the following series: s= 2!/x2 - x4 /4! + 8!/x8 – x16 /16! +....-
x64/64!.
# include <iostream.h>
void main(){ Int X,N,M ,T ,P;
Cin>>x;
N=2;
For (int i=1 ;i<=16 ;i++)
{ T=1;
For(int K =1 ; k<=N ; k++)
T=T*K;
P=1;
M=N*2;
For(int j=1 ; j<=M ; j++)
P=p*j;
S=s+(T/pow(X,N) – pow(X,M)/P);
N=N*4 ;}
Q53: Write a Program that promotes the user to enter the length and width of a rectangle and
print its area.
#include <iostream.h>
void main(){double length,width;
cout<<"Enter the Length an the Width of the rectangle:";
cin>>length>>width;
cout<<"the area of the rectangle is "<<length*width<<endl;}
15
Q54: Write a Program to find the absolute value of an integer.
#include <iostream.h>
void main(){int a;
cout<<"enter a number:";
cin>>a;
if(a<0)
a *= -1;
cout<<"the absolute value of the number is: "<<a<<endl;}
Q55: Write a Program that reads two different integers and print the largest.
#include <iostream.h>
void main(){int a,b;
cout<<"enter two numbers:";
cin>>a>>b;
if(a>b)
cout<<a<<" is the largest"<<endl;
else
cout<<b<<" is the largest"<<endl;}
Q56: Write a Program that reads operation with it's operands and then print the result.
#include <iostream.h>
void main(){double a,b;
char x;
cout<<"enter the operation:";
cin>>a>>x>>b;
if(x=='+')
cout<<a+b<<endl;
else if (x=='-')
cout<<a-b<<endl;
else if (x=='*')
cout<<a*b<<endl;
else if (x=='/')
cout<<a/b<<endl;
else
cout<<"Unknown operation"<<endl;}
Q57: Write a Program to read a character if it's in alphabet then find if its uppercase or
lowercase. If it’s a digit print that it is a digit. Else print that it’s a special character.
#include <iostream.h>
void main(){char x;
cout<<"enter the char:";
cin>>x;
if(x>='A' && x<='Z')
cout<<x<<" is Uppercase char"<<endl;
else if(x>='a' && x<='z')
16
cout<<x<<" is Lowercase char"<<endl;
else if(x>='0' && x<='9')
cout<<x<<" is a Digit"<<endl;
else
cout<<x<<" is a special char"<<endl;}
Q61: Write a loop to find the sum of odd numbers from 20 to 300.
#include <iostream.h>
void main(){int i = 20,sum = 0;
while(i<=300)
{if(i%2==1)
sum += i;
i++; }
cout<<"the sum is: "<<sum<<endl;}
Q62: Find the sum of even number from 1 to 30.
#include <iostream.h>
void main(){int i = 1,sum = 0;
while(i<=30)
{if(i%2==0)
sum += i;
i++;}
cout<<"the sum is: "<<sum<<endl;}
Q66: Write a Program that reads a number and perform the following
- print the number digits in reverse order.
- Find the sum of it's digits.
- Find the average of it's digits.
#include<iostream>
#include<iomanip>
using namespace std;
void main(){int x,y,z=0,avg=0,sum=0;
cin>>x;
while(x!=0)
{y = x%10;
x/=10;
cout<<y<<endl;
sum += y;
18
z++; }
avg = sum/z;
cout<<"the sum is "<<sum<<endl;
cout<<"the avg is "<<avg<<endl;}
Q67: Write a Program to read a set of non-zero(when read zero, stop the program) and find:
- sum
- average
- maximum value
- minimum value
- the number of values
- sum of numbers that divide on 5.
#include<iostream>
#include<iomanip>
using namespace std;
void main(){int x,y,max,min,z=0,avg=0,sum=0,sum5=0;
cin>>x;
max = x;
min = x;
while(x!=0)
{if(x>max)
max = x;
if(x<min)
min =x;
sum+=x;
z++;
if(x%5==0)
sum5+=x;
cin>>x;}
avg = sum/z;
Q68: using functions, write a program to read an integer array with 5 elements and print
the even numbers and odd numbers of it: (repeat all the previous for second array)
#include <iostream.h>
read(int ar[]) {
cout<<"Enter 5 numbers:";
for(int i=0; i<=4; i++)
cin>>ar[i];}
19
isEven(int x) {
if(x % 2 == 0) return 1;
return 0;}
isOdd(int x) {
return (!isEven(x));}
printEven(int ar[]) {
cout<<"Even numbers: ";
for(int i=0; i<=4; i++)
if(isEven(ar[i])) cout<<ar[i]<<" ";
cout<<endl;}
printOdd(int ar[]) {
cout<<"Odd numbers: ";
for(int i=0; i<=4; i++)
if(isOdd(ar[i])) cout<<ar[i]<<" ";
cout<<endl;}
int b[5];
read(b);
printEven(b);
printOdd(b);}
Q69: using functions, write a program to read an integer array with 5 elements and print
the positive numbers and negative numbers of it: (repeat all the previous for second array)
#include <iostream.h>
read(int ar[]) {
cout<<"Enter 5 numbers:";
for(int i=0; i<=4; i++)
cin>>ar[i];}
isPositive(int x) {
if(x >= 0) return 1;
return 0;}
isNegative(int x) {
return (!isPositive(x));}
printPositive(int ar[]) {
20
cout<<"Positive numbers: ";
for(int i=0; i<=4; i++)
if(isPositive(ar[i])) cout<<ar[i]<<" ";
cout<<endl;}
printNegative(int ar[]) {
cout<<"Negative numbers: ";
for(int i=0; i<=4; i++)
if(isNegative(ar[i])) cout<<ar[i]<<" ";
cout<<endl;}
int b[5];
read(b);
printPositive(b);
printNegative(b);}
Q70: using functions, write a program to read an integer array with 5 elements and print the
prime and not prime numbers of it: (repeat all the previous for second array):
#include <iostream.h>
#include <math.h>
read(int ar[]) {
cout<<"Enter 5 numbers:";
for(int i=0; i<=4; i++)
cin>>ar[i];}
isPrimary(int x) {
for(int i=2; i<=sqrt(x); i++)
if(x % i == 0) return 0;
return 1;}
isNotPrimary(int x) {
return (!isPrimary(x));}
printPrimary(int ar[]) {
cout<<"Primary numbers: ";
for(int i=0; i<=4; i++)
if(isPrimary(ar[i])) cout<<ar[i]<<" ";
cout<<endl;}
21
printNotPrimary(int ar[]) {
cout<<"Not Primary numbers: ";
for(int i=0; i<=4; i++)
if(isNotPrimary(ar[i])) cout<<ar[i]<<" ";
cout<<endl;}
int b[5];
read(b);
printPrimary(b);
printNotPrimary(b);}
Q71: using functions, read two (5-real elements) arrays and print the result of the addition,
subtraction, multiplication, and division of each element with the corresponding element in
the second array:
#include <iostream.h>
read(float ar[]) {
cout<<"Enter 5 numbers:";
for(int i=0; i<=4; i++)
cin>>ar[i];}
read(a);
read(b);
22
calc(a, '-', b, c);
print("Subtraction: ",c);
Q72: using functions, read two integer arrays and calculate the average of each one, the sum
of each array elements, the min and max values for each one of them:
#include <iostream.h>
read(int ar[]) {
cout<<"Enter 7 numbers:";
for(int i=0; i<=6; i++)
cin>>ar[i];}
Q73: using functions, read an integer array and search for an element in it:
#include <iostream.h>
char* noName[]={"first","second","third","forth","fifth"};
readArray(int ar[]) {
cout<<"Enter 5 numbers:";
for(int i=0; i<=4; i++)
cin>>ar[i];}
readVal(int &val) {
cout<<"Enter value: ";
cin>>val;}
print(int r) {
if(r != -1)
cout<<"It is "<<noName[r]<<" element.";
else
cout<<"Not found.";}
Q74: using function, read two arrays and sort the first one ascending and the other one
descending:
#include <iostream.h>
read(int ar[]) {
cout<<"Enter 5 numbers:";
for(int i=0; i<=4; i++)
cin>>ar[i];}
sortAsc(int ar[]) {
24
int temp;
for(int x=0; x<=4-1; x++)
for(int y=x+1; y<=4; y++)
if(ar[x]>ar[y]) {
temp = ar[x];
ar[x] = ar[y];
ar[y] = temp;}}
sortDes(int ar[]) {
int temp;
for(int x=0; x<=4-1; x++)
for(int y=x+1; y<=4; y++)
if(ar[x]<ar[y]) {
temp = ar[x];
ar[x] = ar[y];
ar[y] = temp;}}
print(int ar[]) {
for(int i=0; i<=4; i++)
cout<<ar[i]<<" ";
cout<<endl;}
int b[5];
read(b);
sortDes(b);
print(b);}
25
Q1: write a program that read the primary salary and the sales and calculate the total salary which equals the
primary salary plus the commission that is calculated as:
- Comm=2% if the sales are less than or equal three times of the primary salary.
- Comm=3% if the sales are more than three times of the primary salary.
- Comm=5% if the sales are more than five times of the primary salary.
(comm=0.02*bsalary, if(sales<=3*bsalary))
(comm=0.03*bsalary, if(sales>3*bsalary))
(comm=0.05*bsalary, if(sales>5*bsalary))
#include <iostream.h>
main(){float bsalary, sales, comm;
cout<<"basic salary = ";
cin>>bsalary;
cout<<"sales = "; cin>>sales;
if(sales>5*bsalary) comm=0.05*bsalary;
else
if(sales>3*bsalary) comm=0.03*bsalary;
else
if(sales<=3*bsalary) comm=0.02*bsalary;
cout<<"net salary = "<< bsalary + comm;}
Q2: Using functions, write a program to calculate the number of types and quantities of a store (50 type
max.) if you know that the type quantity increased when we choose input process (in) and entering the name
and quantity of that type and decreased if we choose the process (out) by entering the name and quantity of
required out type.
#include<iostream.h>
#include<string.h>
struct ITEM {
char name[20];
int qty;
} ar[50];
int cnt=0;
read(ITEM &val) {
cout<<"Enter item name:";
cin>>val.name;
cout<<"Enter qty:";
cin>>val.qty; }
search(ITEM val, int &r) {
r = -1;
for(int i=0; i<=cnt; i++)
if(strcmp(ar[i].name,val.name)==0) r=i; }
process(char t) {
ITEM val;
read(val);
int r;
search(val,r);
if(t=='i') {
if(r != -1) ar[r].qty += val.qty;
else {
ar[cnt] = val;
cnt++; } }
26
if(t=='o') {
if(r != -1) ar[r].qty -= val.qty;
else cout<<"Item not found!\n"; }}
print() {
for(int i=0; i<cnt; i++)
cout<<ar[i].name<<"\t"<<ar[i].qty<<"\n";}
main() {
int s;
do {
cout<<"(1-in 2-out 3-print 4-exit): ";
cin>>s;
if(s==1) process('i');
if(s==2) process('o');
if(s==3) print();
} while(s!=4); }
#include<iostream.h> 1
void main () {int x,i,j; 333
for (i=1;i<=9;i+=2) 55555
{for (j=1;j<=I;j++) 7777777
cout<<i<<endl; 999999999
for (i=7;i>=1;i-=2) 7777777
{for(j=1;j<=i;j++( 55555
cout<<i<<endl; } 333
1
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{clrscr();
//Declaration part
int a,b,sum=0;
//Processing part
printf("Input the first number\n");
cin>>a;
printf("Input the second number\n");
cin>>b;
sum=a+b;
printf("The Output Is\n");
cout<<sum;
getch();
}
27
int add(int x,int y)
{int c=0;
c=x+y;
return(c);}
void main()
{clrscr();
//Declaration part
int a,b,sum=0;
//Processing part
printf("Input the first number\n");
cin>>a;
/* write a program in c++ to read a letter. if the letter is small convert it to capital
and if it is capital convert it to small */
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{clrscr();
//Declaration part
char ch,c;
//Processing part
printf("Input the character \n");
cin>>ch;
if((ch>='a') && (ch<='z'))
c=ch-32;
else
c=ch+32;
printf("The character after processing Is\n");
cout<<c;
getch();}
/write a programme to add two integer number or sub or mult or div according to user input (like calculator)
*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{clrscr();
//Declaration part
int a,b,s=0;
char c;
//Processing part
printf("Input the first number\n");
28
cin>>a;
printf("Input the second number\n");
cin>>b;
printf("Input the operation appliied on them\n");
cin>>c;
switch(c)
{ case '+':{ s=a+b;
cout<<"Result is "<<s;
break; }
case '-':{ s=a-b;
cout<<"Result is "<<s;
break; }
case '*':{ s=a*b;
cout<<"Result is "<<s;
break; }
case '/':{ s=a/b;
cout<<"Result is "<<s;
break; }
default:cout<<"out of range. so my range is + - * / Only";}
getch();}
29
cin>>b;
for(index=b;index>=1;index--)
p=p*a;
Q: write a program to read two arrays and compare among each corresponding elements in each of them to
produce a new array of the larger correspond of the two entered arrays:
#include<iostream.h>
main(){ int a[3][3],b[3][3],c[3][3];
30
int i,j;
cout<<"enter the first matrix elements"<<endl;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>a[i][j];
cout<<"enter the second matrix elements"<<endl;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>b[i][j];
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{if (a[i][j]>b[i][j])
c[i][j]=a[i][j];
else
c[i][j]=b[i][j];}
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cout<<"the new array of the larger numbers is"<<c[i][j]<<endl;}
Q: write a program to read two 3*3 arrays and print a third array resulted from multiplying the main
diagonal elements of the two read arrays and subtract the other elements of them each element with the
corresponding one:
#include<iostream.h>
main(){ int a[3][3],b[3][3],c[3][3],i,j;
cout<<" enter 9 elements of the first array"<<endl;
for(i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>a[i][j];
cout<<" enter 9 elements of the second array"<<endl;
for(i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>b[i][j];
for(i=0;i<3;i++)
for (j=0;j<3;j++)
{if (i==j)
c[i][j]=a[i][j]*b[j][j];
else
c[i][j]=a[i][j]-b[i][j];}
for(i=0;i<3;i++)
for (j=0;j<3;j++)
cout<<"the new array is c["<<i<<"]["<<j<<"]="<<c[i][j]<<endl;}
Q: write a program in C++ to calculate the positive difference between two matrices 4X4 such that the result
of the difference between any two corresponding numbers in the arrays must be positive:
#include<iostream.h>
void main(){ int a[4][4],b[4][4],c[4][4],i,j;
cout<<"enter the elements of the first matric"<<endl;
for (i=0;i<4;i++)
31
for (j=0;j<4;j++)
cin>>a[i][j];
cout<<"enter the elements of the second matric"<<endl;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
cin>>b[i][j];
for (i=0;i<4;i++)
for (j=0;j<4;j++)
{if (a[i][j]>=b[i][j])
c[i][j]=a[i][j]-b[i][j];
else
c[i][j]=b[i][j]-a[i][j];}
for (i=0;i<4;i++)
for (j=0;j<4;j++)
cout<<"the positive difference of the two matrices is"<<c[i][j]<<endl;}
Q: write a program to calculate the square root of the numbers such as (4,9,16,25,36) that has integer roots
without using the (sqrt) function:
#include<iostream.h>
void main(){ int n,i;
cout<<"enter a number to calculate its square root, zero to terminate"<<endl;
cin>>n;
while(n!=0)
{for (i=1;i<=n;i++)
{if (i*i==n)
cout<<"the square root of"<<n<<"="<<i<<endl;}
cin>>n;}
}
Q: write a program in c++ to convert 2D array to 1D array and fond the maximum number in the new array:
#include<iostream.h>
void main(){int a[2][3], b[6],i,j,k=0,max;
cout<<"enter the matrix elements"<<endl;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
for(i=0;i<2;i++)
for(j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<"\n";
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{b[k]=a[i][j];
k=k+1; }
for(i=0;i<k;i++)
cout<<b[i]<<" ";
cout<<"\n";
max=b[0];
for(i=0;i<k;i++)
{ if (b[i]>max)
max=b[i]; }
cout<<"the maximum number in the new matrix is"<<max ;
cin>>i;}
32
Q: write a program to output the following figure:
1
333
55555
7777777
999999999
7777777
55555
333
1
#include<iostream.h>
void main () {int x,i,j;
for (i=1;i<=9;i+=2)
{for (j=1;j<=i;j++)
cout<<i;
cout<<endl;}
for (i=7;i>=1;i-=2)
{for(j=1;j<=i;j++)
cout<<i;
cout<<endl;}
cin>>x;}
33
4 40 400 4000 40000
5 50 500 5000 50000
#include<iostream.h>
#include<math.h>
void main(){ int i,j,k;
for(i=1;i<=5;i++)
{for(j=0;j<=4;j++)
cout<<i*pow(10,j)<<" ";
cout<<"\n";}
cin>>k;}
Q: write a c++ program to sum two arrays and print the result array in a 2D matrix form:
#include<iostream.h>
void main(){ int a[3][3],b[3][3],c[3][3],i,j;
cout<<"enter 9 elements of the first array"<<endl;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>a[i][j];
cout<<"enter 9 elements of the second array"<<endl;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>b[i][j];
for (i=0;i<3;i++)
for (j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
for (i=0;i<3;i++)
{for (j=0;j<3;j++)
cout<<"c["<<i<<"]["<<j<<"]="<<c[i][j]<<" ";
cout<<endl;}}
34