C++ Assignments
C++ Assignments
Assignment no:01
Qn;write a program which will take the height and length and print out the area of rectangular and the
area of square.
program:
#include<stdio.h>
#include<stdlib.h>
void main()
int height,length,A1,A2;
scanf("%d",&height);
scanf("%d",&length);
A1=height*length;
printf("AREA OF RECTANGULAR:%d\n",A1);
A2=length*length;
printf("AREA OF SQUARE:%d\n",A2);
system("pause");
}
Output:
Joseph bulugu-T/UDOM/2012/00857
Assignment:0 2
Qn;
Write a program which when you enter number from 1 to 12 it print out the month of that
number and the thrice of days of that month by using both switch and if statement.
Program using switch statement:
#include <iostream.h>
#include <conio.h>
void main()
int x,y,z,n,m;
y=30;n=29;m=31;
cin>>x;
switch(x)
case 1:
z=3*y;
cout<<"It is january"<<endl;
break;
case 2:
z==3*n;
cout<<"It is february"<<endl;
break;
case 3:
z=3*y;
cout<<"It is march"<<endl;
break;
case 4:
z=3*m;
cout<<"It is april"<<endl;
break;
case 5:
z=3*y;
cout<<"It is may"<<endl;
break;
case 6:
z=3*m;
cout<<"It is june"<<endl;
break;
case 7:
z=3*y;
cout<<"It is july"<<endl;
break;
case 8:
z=3*y;
cout<<"It is august"<<endl;
break;
case 9:
z=3*m;
cout<<"It is september"<<endl;
break;
case 10:
z=3*y;
cout<<"It is october"<<endl;
break;
case 11:
z=3*m;
cout<<"It is november"<<endl;
break;
case 12:
z=3*y;
cout<<"It is december"<<endl;
break;
default:
cout<<"Out of limit"<<endl;
getch();
}
Output:
Program using if statement:
#include <iostream.h>
#include <conio.h>
void main()
int x,y,z,n,m;
y=31;n=29;m=30;
cin>>x;
if(x==1)
z=3*y;
cout<<"It is january"<<endl;
else if(x==2)
z==3*n;
cout<<"It is february"<<endl;
else if(x==3)
z=3*y;
cout<<"It is march"<<endl;
}
else if(x==4)
z=3*m;
cout<<"It is april"<<endl;
else if(x==5)
z=3*y;
cout<<"It is may"<<endl;
else if(x==6)
z=3*m;
cout<<"It is june"<<endl;
else if(x==7)
z=3*y;
cout<<"It is january"<<endl;
else if(x==8)
z=3*y;
cout<<"It is august"<<endl;
else if(x==9)
z=3*m;
cout<<"It is september"<<endl;
else if(x==10)
z=3*y;
cout<<"It is october"<<endl;
else if(x==11)
z=3*y;
cout<<"It is november"<<endl;
else if(x==12)
z=3*y;
cout<<"It is december"<<endl;
getch();
}
Output;
Joseph bulugu-T/UDOM/2012/00857
Assignment 03:
Qn;
Write a calculator program which accept two numbers and the operator and print out the output.
Program:
#include<iostream.h>
#include<conio.h>
void main()
float x,y,z;
char op,ans;
do
cin>>x;
cin>>y;
cout<<"enter operator"<<endl;
cin>>op;
switch(op)
case '+':
z=x+y;
cout<<"x+y="<<z<<endl;
}break;
case '-':
z=x-y;
cout<<"x-y="<<z<<endl;
}break;
case '*':
z=x*y;
cout<<"x*y="<<z<<endl;
}break;
case '/':
z=x/y;
cout<<"x/y="<<z<<endl;
}break;
cin>>ans;
while(ans=='Y');
getch();
}
Output:
Joseph bulugu-T/UDOM/2012/00857
Assignment 04:
Qn;
Write a program when you entre a number it give the factorior of that number by iteration method.
Program;
#include <iostream.h>
#include <conio.h>
int fact(int n)
int f;
for(int x=n;x>1;x--)
f=n*fact(n-1);
return f;
void main()
int n;
char answer;
do
cout<<"Entre number:"<<endl;
cin>>n;
cout<<n<<"!="<<fact(n)<<endl;
cin>>answer;
}
while(answer=='Y');
getch();
Output;