5. Program to find the largest, smallest, and average of n whole numbers.

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 5:

Write a program to find the largest, smallest, and average of n whole numbers. You can assume that "n" has already been set by the user.

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int l_num, s_num, num;
    float avg, n, sum;
    n=10;
    cout<<"Enter a whole number ";
    cin>>num;
    l_num=s_num=sum=num;
    for(int i=1;i<n;i++)
    {
        cout<<"Enter a whole number ";
        cin>>num;
        if(num>=0)
        {
            if(num>l_num)
                l_num=num;
            if(num<s_num)
                s_num=num;
            sum=sum+num;
        }
        else
            cout<<endl<<"Entered number is not a whole number.";
    }
    avg=sum/n;
    cout<<endl<<"Largest number is: "<<l_num;
    cout<<endl<<"Smallest number is: "<<s_num;
    cout<<endl<<"Average is: "<<avg;
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
4. Program to display alphabets from A to Z using for loop.

Comments