C Program to Find the Sum and Average of Three Numbers

Howdy readers, today you will learn how to write a program to find the sum and average of three numbers using the C Programming language.

C Program to Find the Sum and Average of Three Numbers

The below program prompts the user to enter three integers, then it finds the sum and average of those three entered numbers using simple arithmetic operators.

So, without any delay, let’s begin this tutorial.

C Program to Find the Sum and Average of Three Numbers

C Program

// C Program to Find the Sum and Average of Three Numbers
#include <stdio.h>

int main(){
    float a, b, c;
    float sum, avg;
    
    // Taking input
    printf("Enter the first number: ");
    scanf("%f", &a);
    printf("Enter the second number: ");
    scanf("%f", &b);
    printf("Enter the third number: ");
    scanf("%f", &c);
    
    // Calculating sum
    sum = a + b + c;
    
    // Finding average
    avg = (a + b + c) / 3;
    
    // Displaying result
    printf("The sum of three numbers is: %.2f \n", sum);
    printf("The average of three numbers is: %.2f", avg);
    
    return 0;
}

Output

Enter the first number: 4.2
Enter the second number: 5.7
Enter the third number: 8.1
The sum of three numbers is: 18.00 
The average of three numbers is: 6.00
C Program to Find the Sum and Average of Three Numbers

Explanation

    float a, b, c;
    float sum, avg;

In this program, we have declared five float data type variables named a, b, c, sum and avg.

    printf("Enter the first number: ");
    scanf("%f", &a);
    printf("Enter the second number: ");
    scanf("%f", &b);
    printf("Enter the third number: ");
    scanf("%f", &c);

The program prompts the user to enter three numbers. Here, input is taken with the help of the scanf() function. The entered values get stored in the a, b and c named variables respectively.

    sum = a + b + c;

The sum of three numbers is computed using (+) plus the arithmetic operator.

    avg = (a + b + c) / 3;

Similarly, since there are three numbers, the average is calculated by dividing the sum by 3. The average gets stored in the avg named variable.

    printf("Sum of three numbers is: %.2f \n", sum);
    printf("Average of three numbers is: %.2f", avg);

The sum and average of three numbers are displayed on the screen using the printf() function. Here, \n is a newline character used to jump to the next line. The %.2f format specifier is used to limit the value to 2 decimal places.

Conclusion

I hope after reading this tutorial, you understand how to write a program to find the sum and average of three numbers using the C Programming language.

If you have any queries regarding the tutorial, then let us know in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment