You are on page 1of 9

Fundamental C++ programing chapter 2 worksheet 1

1) Receive a number and determine whether it is odd or even.


#include<iostream.h> Output:
int main(){ enter number
int n; 4
cout<<"enter number required"<<endl; 4=is even number
cin>>n;
enter number
if(n%2==0)
cout<<n<<"=is even number"<<endl;
5
else 5=is odd number
cout<<n<<"=is odd number"<<endl;
}

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

if(a>b) Press Enter to return to Quincy...


cout<<a<<"=is greater than="<<b<<endl;
else
cout<<a<<"=is less than="<<b<<endl;
}

3) add the numbers from 1 to 100 and display the sum


#include<iostream.h> Out puts:
int main(){ the sum is=5050
int i,j;
int sum=0; Press Enter to return to Quincy...
for(i=1;i<=100;i++){
sum=sum+i;}
cout<<"the sum is="<<sum<<endl;
}

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;}

GABST COLLEGE Page 1


Fundamental C++ programing chapter 2 worksheet 1

5) Write a C++ program that will print the following shapes.


#include<iostream.h> Out puts:
int main(){ A
char i,j; AB
for(i='A';i<='E';i++){ ABC
for(j='A';j<=i;j++){ ABCD
cout<<char(j)<<""; ABCDE
}
cout<<endl; Press Enter to return to Quincy...
}}

6) Add the even numbers between 0 and any positive integer number given by the user.

#include<iostream.h> Out puts:


int main(){ enter number
int i,j,n; 10
int sum=0; the sum is=30
cout<<"enter number"<<endl;
cin>>n; Press Enter to return to Quincy...
for(i=0;i<=n;i++){
if(i%2==0){
sum=sum+i;
}}
cout<<"the sum is="<<sum<<endl;
}

7) Find the average of two numbers given by the user.


#include<iostream.h> Out puts:
int main(){ enter two number
int i,j; 2
int average=0; 4
cout<<"enter two number"<<endl; the average of two is=3
cin>>i>>j;
average=(i+j)/2; Press Enter to return to Quincy...
cout<<"the average of two is="<<average<<endl;
}

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;
}

GABST COLLEGE Page 2


Fundamental C++ programing chapter 2 worksheet 1

9) Swap the contents of two variables using a third variable.

#include<iostream.h> Out puts:


int main(){ the number before swap is:
int a=10; a=10
int b=20; b=20
int c; the number after swap is:
cout<<"the number before swap is:"<<endl; a=20
cout<<"a="<<a<<endl; b=10
cout<<"b="<<b<<endl;
cout<<"the number after swap is:"<<endl; Press Enter to return to Quincy...
c=a;
a=b;
b=c;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}

10) Swap the contents of two variables without a third variable.

#include<iostream.h> Out puts:


int main(){ the number before swap is:
int a=10; a=10
int b=20; b=20
cout<<"the number before swap is:"<<endl; the number after swap is:
cout<<"a="<<a<<endl; a=20
cout<<"b="<<b<<endl; b=10
cout<<"the number after swap is:"<<endl;
a=a+b; Press Enter to return to Quincy...
b=a-b;
a=a-b;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<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

#include<iostream.h> Out puts:


int main(){ read 10 integer numbers
int i,n; 10
int sum=0,count=0; 5
cout<<"read 10 integer numbers"<<endl; 6
for(i=1;i<=10;i++){ 78
cin>>n; 70
if(n>50){ 50
count=count+1; 60
sum=sum+n; 80
} 90
} 65
cout<<"the total integer larger than 50 the total integer larger than 50 is=6
is="<<count<<endl; the sum is=443
cout<<"the sum is="<<sum<<endl;

GABST COLLEGE Page 3


Fundamental C++ programing chapter 2 worksheet 1

} Press Enter to return to Quincy...

12) Take an integer from the user and display the factorial of that number

#include<iostream.h> Out puts:


int main(){ enter number from the key board
int i,n; 5
int fact=1; the factorials of:5is=120
cout<<"enter number from the key board"<<endl;
cin>>n; Press Enter to return to Quincy...
for(i=1;i<=n;i++){
fact=fact*i;
}
cout<<"the factorials of:"<<n<<"is="<<fact<<endl;
}

13) Read an integer value from the keyboard and display a message indicating if this number is odd or even.

#include<iostream.h> Out puts:


int main(){ enter number from the key board
int i,n; 8
cout<<"enter number from the key board"<<endl; 8= is even number
for(i=1;i<=10;i++){ 6
cin>>n; 6= is even number
if(n%2==0) 5
cout<<n<<"= is even number"<<endl; 5= is odd number
else 3
cout<<n<<"= is odd number"<<endl; 3= is odd number
}} 2
2= is even number
1
1= is odd number
10
10= is even number
9
9= is odd number
11
11= is odd number
7
7= is odd number

Press Enter to return to Quincy...

14) Find the average, maximum, minimum, and sum of three numbers given by the user.

#include<iostream.h> Out puts:


int main(){ enter three number from the keyboard
int a,b,c,max,min;; 7
int average=0,sum=0; 8
cout<<"enter three number from the keyboard"<<endl; 6
cin>>a>>b>>c; sum=21
sum=a+b+c; average=7
average=(a+b+c)/3; maximun=8

GABST COLLEGE Page 4


Fundamental C++ programing chapter 2 worksheet 1

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;
}}}

GABST COLLEGE Page 5


Fundamental C++ programing chapter 2 worksheet 1

15. Receive three numbers and display them in ascending order from smallest to largest

#include<iostream.h> Out puts:


int main(){ enter three number from the keyboard
int a,b,c,max,min; 7
int average=0,sum=0; 5
cout<<"enter three number from the keyboard"<<endl; 2
cin>>a>>b>>c; numbers in ascending order is=2,5,7
if(a>b){
if(b>c){ Press Enter to return to Quincy...
max=a;
min=c;
cout<<"numbers in ascending order is=";
cout<<c<<","<<b<<","<<a<<endl;
}
else{
max=a;
min=b;
cout<<"numbers in ascending order is=";
cout<<b<<","<<c<<","<<a<<endl;
}}
else if(b>c){
if(c>a){
max=b;
min=a;
cout<<"numbers in ascending order is=";
cout<<a<<","<<c<<","<<b<<endl;
}
else{
max=b;
min=c;
cout<<"numbers in ascending order is=";
cout<<c<<","<<a<<","<<b<<endl;
}
}
else{
if(b>a){
max=c;
min=a;
cout<<"numbers in ascending order is=";
cout<<a<<","<<b<<","<<c<<endl;
}
else{
max=c;
min=b;
cout<<"numbers in ascending order is=";
cout<<b<<","<<a<<","<<c<<endl;
}}}

GABST COLLEGE Page 6


Fundamental C++ programing chapter 2 worksheet 1

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

#include<iostream.h> Out puts:


int main(){ the sum is=275
int i,j,sum=0;
for(i=5;i<=50;i++){ Press Enter to return to Quincy...
if(i%5==0)
sum=sum+i;
}
cout<<"the sum is="<<sum<<endl;
}
3) write an application to print out the numbers 10 through 49 in the following manner
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
#include<iostream.h> Out puts:
int main(){ 10 11 12 13 14 15 16 17 18 19
int i,j; 20 21 22 23 24 25 26 27 28 29
for(i=10;i<20;i++){ 30 31 32 33 34 35 36 37 38 39
cout<<i<<" "; 40 41 42 43 44 45 46 47 48 49
} Press Enter to return to Quincy...
cout<<endl;
for(i=20;i<30;i++){
cout<<i<<" ";
}
cout<<endl;
for(i=30;i<40;i++){
cout<<i<<" ";
}
cout<<endl;
for(i=40;i<50;i++){
Co cout<<i<<" ";

GABST COLLEGE Page 7


Fundamental C++ programing chapter 2 worksheet 1

}}

4) Write a C++ program that will print the following shapes.


#include<iostream.h> Out puts:
int main(){
int i,j,c=1,k;
for(i=1;i<=5;i++){
cout<<endl;
for(k=4;k>=i;k--)
cout<<" ";
for(j=1;j<=c;j++){
cout<<"*";
}
c=c+2;
}
c=7;
for(i=1;i<=5;i++){
cout<<endl;
for(k=1;k<=i;k++)
cout<<" ";
for(j=1;j<=c;j++){
cout<<"*";
}
c=c-2;
}}
5) Write a C++ program that will print the following shapes.
#include<iostream.h>
#include<string.h> Out puts:
int main(){
int i,j,c=1,k;
for(i=1;i<=5;i++){
cout<<endl;
for(k=4;k>=i;k--)
cout<<" ";
for(j=1;j<=c; j++){
cout<<"*";
}
c=c+2;
}}
6) Write a C++ program that will print the following shapes.
#include<iostream.h> Out puts:
#include<string.h>
int main(){
int i,j,c=5,k;
for(i=1;i<=5;i++){
cout<<endl;
for(j=1;j<=c;j++){
cout<<"*";
}
c--;
}}
GABST COLLEGE Page 8
Fundamental C++ programing chapter 2 worksheet 1

7) Write a C++ program that will print the following shapes.

#include<iostream.h> Out puts:


int main(){
int i,j,k;
for(i=1;i<=5;i++){
cout<<endl;
for(k=1;k<=2;k++){
cout<<" ";}
for(j=1;j<=i;j++){
cout<<"*";
}}}

8) Write a C++ code to display only even numbers found between 0 and 20.

#include<iostream.h> Out puts:


int main(){
int i,j; ===========================================
cout<<"==================================="<<endl; 2,4,6,8,10,12,14,16,18,20,
for(i=1;i<=20;i++){
if(i%2==0) Press Enter to return to Quincy...
cout<<i<<",";
}
cout<<endl;
cout<<"==================================="<<endl;
}
9) Write a C++ code to compute the following products: 1*2*3*…*20.

#include<iostream.h> Out puts:


int main(){ =======================================
int i,j,product=1; the products of the number is=-2102132736
for(i=1;i<=20;i++){ =======================================
product=product*i;
} Press Enter to return to Quincy...
cout<<"==========================================="<<endl;
cout<<"the products of the number is="<<product<<endl;
cout<<"==========================================="<<endl;
}

GABST COLLEGE Page 9

You might also like