Professional Documents
Culture Documents
7. Find the average, maximum, minimum, and sum of three numbers given by the user.
#include<iostream.h>
#include<conio.h>
float f,s,t,sum,avg,max,min;
void main()
{ clrscr();
cout<<"Enter any three numbers: ";
cin>>f>>s>>t;
sum=f+s+t;
avg=sum/3;
if((f>s)&&(f>t)) max=f;
if((s>f)&&(s>t)) max=s;
if((t>f)&&(t>s)) max=t;
if((f<s)&&(f<t)) min=f;
if((s<f)&&(s<t)) min=s;
if((t<f)&&(t<s)) min=t;
cout<<"The sum is "<<sum<<endl;
cout<<"The average is "<<avg<<endl;
cout<<"The maximum is "<<max<<endl;
cout<<"The minimum is "<<min<<endl;
getch();
}
8. Find the area of a circle where the radius is provided by the user.
#include<iostream.h>
#include<conio.h>
float r,a;
void main()
{ clrscr();
cout<<"Enter radius: ";
cin>>r;
a=(22/7)*r*r;
cout<<"The area is "<<a;
getch();
}
9. Swap the contents of two variables using a third variable.
#include<iostream.h>
#include<conio.h>
int a,b,t;
void main()
{ clrscr();
cout<<"Enter a ";
Note:
t=a means put the value of a in t.
cin>>a;
cout<<"Enter b ";
cin>>b;
t=a;a=b;b=t;
cout<<"a now is "<<a<<endl;
cout<<"b now is "<<b<<endl;
getch();
}
10. Swap the content of two variables without using a third variable.
#include<iostream.h>
#include<conio.h>
int a,b;
void main()
{ clrscr();
cout<<"Enter a ";
cin>>a;
cout<<"Enter b ";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"a now is "<<a<<endl;
cout<<"b now is "<<b<<endl;
getch();
}
Note:
Initially, a=54 and b=33
After a=a+b
a=54+33=87
After b=a-b
b=87-33=54
After a=a-b
a=87-54=33
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>
#include<conio.h>
int i,n,c;
void main()
{ clrscr();
c=0;
for(i=1;i<=10;++i)
{cout<<"Enter an integer between 1
and 100 ";
cin>>n;
if(n>50) ++c;
}
cout<<endl<<"You entered "<<c<<"
numbers that are >50";
getch();
}
12. Take an integer from the user and display the factorial of that number.
#include<iostream.h>
#include<conio.h>
int i,n,f;
void main()
{ clrscr();
cout<<"Enter the integer to which you
want factorial ";
cin>>n;
f=1;
for(i=1;i<=n;++i)
f=f*i;
cout<<endl<<"Its factorial is "<<f;
getch();
}