50% found this document useful (2 votes)
2K views15 pages

C++ Assignments

The document contains four programming assignments submitted by Joseph bulugu-T/UDOM/2012/00857. The first assignment involves writing a program to calculate the area of a rectangle and square given height and length. The second assignment involves writing a program to print the month name and thrice of its days given a number from 1 to 12 using switch and if statements. The third assignment involves writing a calculator program to calculate basic mathematical operations given two numbers and an operator. The fourth assignment involves writing a program to calculate the factorial of a given number using an iterative method.

Uploaded by

Joseph bulugu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
2K views15 pages

C++ Assignments

The document contains four programming assignments submitted by Joseph bulugu-T/UDOM/2012/00857. The first assignment involves writing a program to calculate the area of a rectangle and square given height and length. The second assignment involves writing a program to print the month name and thrice of its days given a number from 1 to 12 using switch and if statements. The third assignment involves writing a calculator program to calculate basic mathematical operations given two numbers and an operator. The fourth assignment involves writing a program to calculate the factorial of a given number using an iterative method.

Uploaded by

Joseph bulugu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Joseph bulugu- T/UDOM/2012/00857

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;

printf("Entre the value of height:\n");

scanf("%d",&height);

printf("Entre the value of length:\n");

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;

cout<<"Entre the month"<<endl;

cin>>x;

switch(x)

case 1:

z=3*y;

cout<<"It is january"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 2:

z==3*n;

cout<<"It is february"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 3:
z=3*y;

cout<<"It is march"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 4:

z=3*m;

cout<<"It is april"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 5:

z=3*y;

cout<<"It is may"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 6:

z=3*m;

cout<<"It is june"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 7:

z=3*y;

cout<<"It is july"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 8:

z=3*y;

cout<<"It is august"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;
case 9:

z=3*m;

cout<<"It is september"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 10:

z=3*y;

cout<<"It is october"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 11:

z=3*m;

cout<<"It is november"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

case 12:

z=3*y;

cout<<"It is december"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

break;

default:

cout<<"Out of limit"<<endl;

cout<<"The thrice of the days:"<<y<<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;

cout<<"Entre the month:"<<endl;

cin>>x;

if(x==1)

z=3*y;

cout<<"It is january"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

else if(x==2)

z==3*n;

cout<<"It is february"<<endl;

cout<<"The thrice of the days:"<<z<<endl;

else if(x==3)

z=3*y;

cout<<"It is march"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

}
else if(x==4)

z=3*m;

cout<<"It is april"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

else if(x==5)

z=3*y;

cout<<"It is may"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

else if(x==6)

z=3*m;

cout<<"It is june"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

else if(x==7)

z=3*y;

cout<<"It is january"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

else if(x==8)

z=3*y;

cout<<"It is august"<<endl;

cout<<"The thrice of the day:"<<z<<endl;


}

else if(x==9)

z=3*m;

cout<<"It is september"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

else if(x==10)

z=3*y;

cout<<"It is october"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

else if(x==11)

z=3*y;

cout<<"It is november"<<endl;

cout<<"The thrice of the day:"<<z<<endl;

else if(x==12)

z=3*y;

cout<<"It is december"<<endl;

cout<<"The thrice of the day:"<<z<<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

cout<<"enter the first number"<<endl;

cin>>x;

cout<<"enter the second number"<<endl;

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;

cout<<"do you want to continue?/N/Y/"<<endl;

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;

cout<<"do you want to continue?Y/N"<<endl;

cin>>answer;
}

while(answer=='Y');

getch();

Output;

You might also like