C Language

June 6, 2023

How to use sum and average in C language


#include <stdio.h>
int main()
{
    int i = 1, n, sum = 0;
    float avg;
    printf("enter 3 positive number : \n");
    while (i <= 3)
    {
        printf("enter number %d : ", i);
        scanf("%d", &n);
        if (n < 0)
        {
            printf("enter only positive numbers\n");
            continue;
        }
        sum += n;
        i++;
    }
    avg = sum / 10.0;
    printf("sum=%d avg=%.2f\n", sum, avg);
    return 0;
}

Output


enter number : 1 2
enter number : 2 4 
enter number : 3 5
sum=11 avg=1.10

by : Nadeem Khan

Quick Summary: