Dremendo Tag Line

Input 10 numbers and find average of positive numbers using while loop in C++

while Loop - Question 4

In this question, we will see how to input 10 numbers and find the average of only positive numbers in C++ programming using while loop. To know more about while loop click on the while loop lesson.

Q4) Write a program in C++ to input 10 numbers and find the average of only positive numbers using while loop.

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=1,n,sum=0,c=0;
    float avg=0;

    cout<<"Enter 10 numbers\n";
    while(i<=10)
    {
        cin>>n;
        if(n>0)
        {
            sum=sum+n;
            c=c+1;
            avg=sum/(float)c;
        }

        i=i+1;
    }
    cout<<"Average of positive numbers = "<<avg;
    return 0;
}

Output

Enter 10 numbers
-12
-6
12
-4
20
3
-89
-51
-7
5
Average of positive numbers = 10
video-poster