Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1.

Program to calculate area of rectangle by using two function that prompt the
user to enter length and height of rectangle
#include<iostream>
using namespace std;
float area(float l,float h);
int main()
{
float length,height;
cout<<"Enter length of the rectangle";
cin>>length;
cout<<"Enter height of the rectangle";
cin>>height;
area(length,height);
}
float area(float l,float h)
{
float area;
area=l*h;
cout<<"Area of rectangle is "<<area;
}

2.A C++ program that calculate area of the circle with radius 24cm or volume of the
cylinder with radius 40cm and height 60cm depending on the user’s choice by using switch
case and three function.
#include<iostream>
using namespace std;
float circle_A(float r);
float volume_C(float R,float h);
float pi=3.14;
int main()
{
int choice;
float R,r,h;
R=40;
r=24;
h=60;
cout<<" MENU "<<endl;
cout<<"1: To calculate area of the circle "<<endl;
cout<<"2: To calculate volume of the cylinder"<<endl;
cout<<"Enter your choice "<<endl;
cin>>choice;

switch(choice)
{
case 1:
circle_A(r);
break;

case 2:
volume_C(R,h);
break;
default:
cout<<"Invalid choice";
break;
}
}
float circle_A(float r)
{
float A;
A=pi*r*r;
cout<<"Area of the circle = "<<A;
}
float volume_C(float R,float h)
{
float V;
V=pi*R*R*h;
cout<<"The volume of the cylinder = "<<V;
}
3.A C++ program for comparison between two numbers by using two
functions.

#include<iostream>
using namespace std;
int comparison(int x,int y);
int main()
{
int p,q;
cout<<"Enter first number for comparison"<<endl;
cin>>p;
cout<<"Enter second number for comparison"<<endl;
cin>>q;
comparison(p,q);
}
int comparison(int x,int y)
{
if(x>y)
{
cout<<x<<"is greater than"<<y;
}
else if(x<y)
{
cout<<x<<"is less than"<<y;
}
else
{
cout<<x<<" is equal to "<<y;
}
}
4.A C++ program that prompts the user to enter marks of 8 students and
display entered marks

#include<iostream>
using namespace std;
int main()
{
int i,marks[8];
cout<<"Enter marks for 8 students"<<endl;
for(i=0;i<8;i++)
{
cin>>marks[i];
}
cout<<"Entered marks are: "<<endl;
for(i=0;i<8;i++)
{
cout<<marks[i]<<endl;
}
return 0;
}
4.A C++ program that prompts the user to enter marks of 8 students and
display entered marks, sum, average and highest score

#include<iostream>
using namespace std;
int main()
{
int i,marks[8],sum=0,avrg,highestscore=marks[0];
cout<<"Enter marks for 8 students"<<endl;
for(i=0;i<8;i++)
{
cin>>marks[i];
}
cout<<"Entered marks are :"<<endl;
for(i=0;i<8;i++)
{
cout<<marks[i]<<endl;
sum=sum+marks[i];
avrg=sum/8;
if(marks[i]>highestscore)
{
highestscore=marks[i];
}
}
cout<<"Sum = "<<sum<<endl;
cout<<"Average = "<<avrg<<endl;
cout<<"Highest score = "<<highestscore;
return 0;
}
5.A C++ program to display largest number from initialized array.

#include<iostream>
using namespace std;
int main()
{
int num[]={20,31,19,70,69,40,90},i;
int big=num[0];
for(i=0;i<7;i++)
{
if(num[i]>big)
{
big=num[i];
}
}
cout<<"The largest number is "<<big;
return 0;
}
6.Program to find factorial of a number.
#include<iostream>
using namespace std;
int main()
{
int i,n,prod=1;
cout<<"enter number to find factorial"<<endl;
cin>>n;
for(i=n;i>=1;i--)
{
prod=prod*i;
}
cout<<n<<"i"<<"="<<prod;
return 0;
}

7.Program to convert meter to kilometer and vice versa using three functions
depending on the user choice
#include<iostream>
using namespace std;
float MeterTokilometer(float x);
float kilometerToMeter(float y);
int main()
{
float num1,num2,ans,soln;
int choice;
cout<<"press 1 to convert meter to kilometer"<<endl;
cout<<"press 2 to convert kilometer to meter"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;

switch(choice)
{
case 1:
cout<<"enter number in meter"<<endl;
cin>>num1;
ans=MeterTokilometer(num1);
cout<<num1<<"M"<<"="<<ans<<"KM";
break;

case 2:
cout<<"enter number in kilometer"<<endl;
cin>>num2;
soln=kilometerToMeter(num2);
cout<<num2<<"KM"<<"="<<soln<<"M";
break;

default:
cout<<"Invalid choice";
}
}
float MeterTokilometer(float x)
{
float result;
result=x/1000;
return result;
}
float kilometerToMeter(float y)
{
float answer;
answer=y*1000;
return answer;
}
8.Program to prompt user to enter marks of 370 students and find
highest score from entered marks
#include<iostream>
using namespace std;
int main()
{
int i, marks[370];
int highestscore=marks[0];
cout<<"Enter marks for 370 students"<<endl;
for(i=0;i<370;i++)
{
cin>>marks[i];
}
for(i=0;i<370;i++)
{
if(marks[i]>highestscore)
{
highestscore=marks[i];
}
}
cout<<"Highest score is "<<highestscore;
return 0;
}
9.A C++ program to display
8x5=40
40÷8=5

#include<iostream>
using namespace std;
int main()
{
int x=8,y=5;
int ans,sol;
ans=x*y;
sol=ans/x;
cout<<x<<"x"<<y<<"="<<ans<<endl;
cout<<ans<<"÷"<<x<<"="<<sol;
return 0;
}
10.A C++ program to display initials of the firstname
,middlename and surname.
#include<iostream>
using namespace std;
int main()
{
char firstname[20]="RAPHAEL";
char middlename[20]="ISAKA";
char surname[20]="PETRO";
cout<<firstname[0]<<" "<<middlename[0]<<" "<<surname[0];
return 0;
}

You might also like