You are on page 1of 5

Solutions to Lab Exercises FOP

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


#include<iostream.h>
#include<conio.h>
int n;
void main()
{clrscr();
Note: n%2 gives the remainder
cout<<"Enter a number ";
obtained after dividing n with 2.
cin>>n;
if(n%2==0) cout<<n<<" is Even";
else cout<<n<<" is Odd";
getch();
}
2. Obtain two numbers from the keyboard, and determine and display which (if either)
is the larger of the two numbers.
#include<iostream.h>
#include<conio.h>
int a,b;
void main()
{ clrscr();
cout<<"Enter any two integers";
cin>>a>>b;
if(a>b) cout<<a<<" is greater than "<<b;
else if(b>a) cout<<b<<" is greater than "<<a;
else cout<<"both are equal";
getch();
}
3. Receive 3 numbers and display them in ascending order from smallest to largest
#include<iostream.h>
#include<conio.h>
int a,b,c;
void main()
{ clrscr();
cout<<"Enter any three integers:";
cin>>a>>b>>c;
if((a<b)&&(b<c)) cout<<a<<" "<<b<<" "<<c;
if((a<c)&&(c<b)) cout<<a<<" "<<c<<" "<<b;
if((b<a)&&(a<c)) cout<<b<<" "<<a<<" "<<c;
if((b<c)&&(c<a)) cout<<b<<" "<<c<<" "<<a;
if((c<a)&&(a<b)) cout<<c<<" "<<a<<" "<<b;
if((c<b)&&(b<a)) cout<<c<<" "<<b<<" "<<a;
getch();
}

4. Add the numbers from 1 to 10 and display the sum


#include<iostream.h>
#include<conio.h>
int i,s;
void main()
{ clrscr();
s=0;
for(i=1;i<=10;++i)
{ s+=i;
cout<<"i="<<i<<" and s="<<s<<"\n";
}
cout<<"\n Total is "<<s;
getch();
}
5. Add the even numbers between 0 and any positive integer number given by the user.
#include<iostream.h>
#include<conio.h>
int i,n,esum;
void main()
{ clrscr();
cout<<"Enter any positive number: ";
cin>>n; esum=0;i=0;
while(i<=n)
{ esum+=i; i+=2;}
cout<<"Sum of even numbers between
"<<0<<" and "<<n <<" is:"<<esum;
getch();
}
6. Find the average of two numbers given by the user.
#include<iostream.h>
#include<conio.h>
float f,s,a;
void main()
{ clrscr();
cout<<"Enter any two numbers: ";
cin>>f>>s;
a=(f+s)/2;
cout<<"The average of these two numbers is
"<<a;
getch();
}

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

a=b means put the value of b in a.


b=t means put the value of t in b.

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

You might also like