Saturday, 10 December 2016

Write a program to find largest, smallest and average of "n" whole numbers. You can assume that "n" has already been set by the user. Using While Loop. (C++)

Source Code:

#include<iostream>
using namespace std;
int main ()
{
int x,a=1,z,sum=0,lar,sm;
float avg;
cout<<"How many numbers you want to enter: ";
cin>>x;
cout<<"Enter Numbers: ";
while(a<=x){
cin>>z;
if(z>lar){
lar=z;
}
if(z<lar){
sm=z;
}
sum=sum+z;
a++;
}
avg=sum/x;
cout<<"Average of your numbers: "<<avg<<endl;
cout<<"Largest Number: "<<lar<<endl;
cout<<"Smallest Number: "<<sm<<endl;
}


3 comments:

  1. Bro there is problem in your logic... Plz modified it for the future of all upcoming generation... 😢

    ReplyDelete
  2. #include
    using namespace std;
    int main()
    {
    int len,max,min,i=2,num,sum=0;
    float avg;
    cout<<"How many nunber you want to enter:";
    cin>>len;
    cout<<"Enter any whole number:\n";
    cin>>num;
    max=min=num;
    while(i<=len)
    {
    cin>>num;
    if(num>max)
    max=num;
    if(num<min)
    min=num;
    sum=sum+num;
    i++;
    }
    avg=sum/len;
    cout<<"The largest number is:"<<max<<endl;
    cout<<"The smallest number is:"<<min<<endl;
    cout<<"sum of all number is:"<<sum<<endl;
    cout<<"Average of all number is:"<<avg;
    }

    ReplyDelete