1.WAP To Find Average of N Numbers.: Output

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1.WAP to find average of n numbers.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int i,a[30],n;
float avg,sum=0;
cout<<"\n Enter the number of elements whose average is to be found ";
cin>>n;
cout<<"\n Enter the numbers ";
for(i=0;i<n;i++)
{ cin>>a[i];
sum=sum+a[i];
}
avg=sum/n;
cout<<"\n Average = "<<avg;

OUTPUT :
Enter the number of elements whose average is to be found : 5
Enter the numbers 3 7 4 9 10
Average = 6.6

2. WAP to calculate simple interest .


#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float p,r,t,s;
cout<<"\n Enter principle amount ";
cin>>p;
cout<<"\n Enter rate of interest per annum ";
cin>>r;
cout<<"\n Enter time in years ";
cin>>t;
s=(p*r*t)/100;
cout<<"\n Simple Interest = "<<s;

OUTPUT :
Enter principle amount in Rs.: 1200
Enter rate of interest per annum: 5
Enter time in years: 3
Simple Interest = Rs. 180

3. WAP to swap the value of two variables .


(1) Using 3rd variable :
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float a,b,t;
cout<<"\n Enter two numbers: ";
cin>>a>>b;
cout<<"\n Value of a and b : "<<a<<" "<<b;
t=a;
a=b;
b=t;
cout<<"\n Swapped values of a and b : "<<a<<" "<<b;

OUTPUT:
Enter two numbers : 4 7
Value of a and b : 4 7
Swapped values of a and b : 7 4

(2) Without using 3rd variable :

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float a,b,t;
cout<<"\n Enter two numbers: ";
cin>>a>>b;
cout<<"\n Value of a and b : "<<a<<" "<<b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\n Swapped values of a and b : "<<a<<" "<<b;

OUTPUT:
Enter two numbers : 3 7
Value of a and b : 3 7
Swapped values of a and b : 7 3

5. WAP to check whether a no is even or odd.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a;
cout<<"\n Enter a number: ";
cin>>a;
if(a%2==0)
{
cout<<"\n Number is even " ;
}
else
{
cout<<"\n Entered number is odd " ;
}
}

OUTPUT:
Enter a number: 7
Entered number is odd

7. WAP to sum the digits of a number.


#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,p,sum=0;
cout<<"\n Enter a number: ";
cin>>n;
while(n!=0)
{
p=n%10 ;
sum=sum+p;
n=n/10;
}
cout<<" Sum of digits = "<<sum;
}
OUTPUT:
Enter a number: 1234
Sum of digits = 10

8. WAP to reverse the digits of a number.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,p,rev=0;
cout<<"\n Enter a number: ";
cin>>n;
while(n!=0)
{
p=n%10 ;
rev=rev*10+p;
n=n/10;
}
cout<<" Reverse = "<<rev;
}

OUTPUT:
Enter a number: 1234
Reverse = 4321

You might also like