C++ program to get the Arithmetic Mean of first 10 natural numbers (from 1 to 10) using FOR loop, WHILE loop and DO WHILE loop:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i, a;
float mean, sum=0;
cout<<"Mean of first 10 natural rumbers (1 to 10): ";
i=1;
cout<<"\n\nEnter your choice:"<<"\n"<<"1-> Using for loop"<<"\n"<<"2-> Using while loop"<<"\n"<<"3-> Using do...while loop"<<"\n\n";
cin>>a;
switch(a)
{
case 1:
for(i=1; i<=10; i++)
sum=sum+i;
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 2:
while(i<=10)
{
sum=sum+i;
i++;
}
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 3:
do
{
sum=sum+i;
i++;
}
while(i<=10);
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
default:
break;
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i, a;
float mean, sum=0;
cout<<"Mean of first 10 natural rumbers (1 to 10): ";
i=1;
cout<<"\n\nEnter your choice:"<<"\n"<<"1-> Using for loop"<<"\n"<<"2-> Using while loop"<<"\n"<<"3-> Using do...while loop"<<"\n\n";
cin>>a;
switch(a)
{
case 1:
for(i=1; i<=10; i++)
sum=sum+i;
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 2:
while(i<=10)
{
sum=sum+i;
i++;
}
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 3:
do
{
sum=sum+i;
i++;
}
while(i<=10);
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
default:
break;
}
getch();
}
No comments:
Post a Comment