How to Calculate Average Input by User in C Program

calculate average by c programing language

Introduction :

Calculating the average of input by the user is a basic task in programming. Using the C programming language, you can create a simple C program to calculate the average of multiple numbers easily. In this article, I shall show you how to calculate the average of multiple numbers in C programming language. In this program, user has to input some numbers. Then, it displays the average on the console screen.

What is average :

Average is known as the mean of some numbers. It is a mathematical concept by which you can calculate the central value of given numbers. To find the average of the given numbers, you have to add together all the numbers and divided by the total number of values (count).

The average is commonly used in various fields like statistics, mathematics, grading systems, financial calculations and more. The following formula is used to calculate the average of multiple numbers.

average = sum of numbers / count of numbers

About the program :

This is a C program that calculates the average of multiple numbers entered by the user. The program asks you how many numbers you want to calculate the average for. If you enter zero (0), it asks you to enter a number greater than zero (0). After your input, it again asks you to enter the numbers one by one. Then, it calculates the average and display the result on the console screen.

Explanation of the program :

At first, include stdio.h for using input and output functions. In the main() function, declare integer variables such as “count_num” and “i”. You have to also declare float variables such as “float total”, “num_input” and “avg_num”. Using printf() function, ask the user to enter input numbers. To get the input value, you can use scanf() function.

Here, you can use “if else” statement to check if the user entered zero (0) or not. If the user enters a number greater than zero (0), calculate the average by dividing total numbers by count number. Then, display the average of the input numbers on the screen.

How run the program :

To calculate average of numbers input from user, you have to install VS Code on your PC. Then, create a new C file (average.c) in it. Now, copy the below code and paste it in the file and save.

Source code of the program :

The following code is the source code of calculating the average program in C programming language.

/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
    int count_num, i;
    float total = 0, num_input, avg_num;
    printf("How many number you want to find average: ");
    scanf("%d", &count_num);
    if (count_num == 0)
    {
        printf("Enter greater than 0\n");
    }
    else
    {
        for (i = 1; i <= count_num; i++)
        {
            printf("Enter %d number : ", i);
            scanf("%f", &num_input);
            total += num_input;
        }
        avg_num = total / count_num;
        printf("The average of %d numbers is: %.2f\n", i - 1, avg_num);
    }
    return 0;
}

Output of the program :

If you run the program, you can see the below output on your PC.

Output of average program by c

Conclusion :

At last, you have learned how to create a program that calculates the average input numbers by user in the C programming language. Thank you for visiting my site.

Scroll to Top