Professional Documents
Culture Documents
2) Obtain two numbers from the keyboard, and determine and display which (if either) is the larger of the two numbers.
#include<iostream.h>
int main(){ Output:
int a,b; enter the first number
cout<<"enter the first number "<<endl; 8
cin>>a; enter the second number
cout<<"enter the second number "<<endl; 5
cin>>b; 8=is greater than=5
4) Write a c++ program that compute the following series sum=1! +2! +3! +----+n!
#include<iostream.h> Out puts:
int main(){ enter number
int i,j,n; 4
cout<<"enter number"<<endl; the sum is=33
cin>>n;
int sum=0; Press Enter to return to Quincy...
for (i=1;i<=n;i++){
int fact=1;
for(j=i;j>=1;j--){
fact=fact*j;}
sum=sum+fact;
}
cout<<"the sum is="<<sum<<endl;}
6) Add the even numbers between 0 and any positive integer number given by the user.
8) Find the area of a circle where the radius is provided by the user.
#include<iostream.h> Out puts:
int main(){ enter radius of a circle
int area=0; 8
int pi=3.14; the area of circle is=192
int r;
cout<<"enter radius of a circle"<<endl; Press Enter to return to Quincy...
cin>>n;
area=pi*r*r;
cout<<"the area of circle is="<<area<<endl;
}
11) read 10 integers from the keyboard in the range 0 - 100, and count how many of them are larger than 50, and display
this result
12) Take an integer from the user and display the factorial of that number
13) Read an integer value from the keyboard and display a message indicating if this number is odd or even.
14) Find the average, maximum, minimum, and sum of three numbers given by the user.
if(a>b){ minimum=6
if(b>c){
max=a; Press Enter to return to Quincy...
min=c;
cout<<"sum="<<sum<<endl;
cout<<"average="<<average<<endl;
cout<<"maximun="<<max<<endl;
cout<<"minimum="<<min<<endl;
}
else{
max=a;
min=b;
cout<<"sum="<<sum<<endl;
cout<<"average="<<average<<endl;
cout<<"maximun="<<max<<endl;
cout<<"minimum="<<min<<endl;
}}
else if(b>c){
if(c>a){
max=b;
min=a;
cout<<"sum="<<sum<<endl;
cout<<"average="<<average<<endl;
cout<<"maximun="<<max<<endl;
cout<<"minimum="<<min<<endl;
}
else{
max=b;
min=c;
cout<<"sum="<<sum<<endl;
cout<<"average="<<average<<endl;
cout<<"maximun="<<max<<endl;
cout<<"minimum="<<min<<endl;
}
}
else{
if(b>a){
max=c;
min=a;
cout<<"sum="<<sum<<endl;
cout<<"average="<<average<<endl;
cout<<"maximun="<<max<<endl;
cout<<"minimum="<<min<<endl;
}
else{
max=c;
min=b;
cout<<"sum="<<sum<<endl;
cout<<"average="<<average<<endl;
cout<<"maximun="<<max<<endl;
cout<<"minimum="<<min<<endl;
}}}
15. Receive three numbers and display them in ascending order from smallest to largest
1) Prime number is an integer greater than one and divisible only by itself and one. The first seven prime numbers are 2,
3, 5, 7, 11, 13, and 17.
Write a program that displays all the prime numbers between Out puts:
1 and 100. 2=is prime number
#include<iostream.h> 3=is prime number
int main(){ 4=is composite number
int i,j,c; 5=is prime number
for(i=2;i<=100;i++){ 6=is composite number
c=0; 7=is prime number
for(j=1;j<=i;j++){ 8=is composite number
if(i%j==0) 9=is composite number
c++; 10=is composite number
}
if(c<=2) Press Enter to return to Quincy...
cout<<i<<"=is prime number"<<endl;
else
cout<<i<<"=is composite number"<<endl;
}}
2) Write for loop statements to compute the following sums. a. 5+10+15+...+50
}}
8) Write a C++ code to display only even numbers found between 0 and 20.