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 <stdio.h>
#include <conio.h>

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

    printf("Enter 10 numbers\n");
    while(i<=10)
    {
        scanf("%d",&n);
        if(n>0)
        {
            sum=sum+n;
            c=c+1;
            avg=sum/(float)c;
        }

        i=i+1;
    }
    printf("Average of positive numbers = %f",avg);
    return 0;
}

Output

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