C Program to Find Sum and Average of 3 Numbers

C Program to Find Sum and Average of 3 Numbers | Program description:- Write a C program to find the sum and average of three numbers. Take inputs from the end-user.

Before developing a C program to find the sum and average of 3 numbers, let us see how to find the sum and average of three numbers.

Let three numbers are a, b & c then,
Sum = (a+b+c)
and,
Average = sum/3

C Program to Find Sum and Average of 3 Numbers

#include<stdio.h>
int main()
{
  // declare variable
  float num1, num2, num3;
  float sum, avg;

  // take inputs
  printf("Enter three Numbers: ");
  scanf("%f %f %f",&num1, &num2, &num3);

  // calculate sum
  sum = num1 + num2 + num3;

  // calculate average
  avg = sum / 3;

  // display entered numbers
  printf("Entered numbers are: %.2f, %.2f and %.2f\n",
           num1, num2, num3);

  // display sum and average
  printf("Sum=%.2f\n", sum);
  printf("Average=%.2f\n",avg );

  return 0;
}

Output for the different test-cases:-

Enter three Numbers: 10 20 30
Entered numbers are: 10.00, 20.00 and 30.00
Sum=60.00
Average=20.00

Enter three Numbers: 1.9 2.5 8.16
Entered numbers are: 1.90, 2.50 and 8.16
Sum=12.56
Average=4.19

In this program, three variables are declared num1, num2, and num3. These variables will store three floating-point numbers. The printf function displays the messages to the user “Enter three Numbers:”. Later using scanf function three numbers are stored in the variables num1, num2, and num3.

The scanf function reads the numbers until the user enters not enters the three numbers. If the user enters integer number then it will be automatically converted to a floating-point number by type promotion.

The sum is calculated as num1+num2+num3. The values of those three variables are floating-point numbers so, their sum will be a floating-point number. The average is calculated as sum/(total numbers).

After calculating the sum and average of the numbers, the variables num1, num2, and num3 are printed. Finally, the sum and average are displayed to the screen.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Similar Basic C programming examples

Leave a Comment

Your email address will not be published. Required fields are marked *