Call us at 0700-922-6559 or Click here

Write a program to calculate the average of three real numbers.

#include<stdio.h>

int main()

  int a,b,c, sum; 

  float d; 

  printf(“Please enter 3 numbers”); 

  scanf(“%d%d%d”,&a,&b,&c); 

  sum=a+b+c;

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

  printf(“\nSum is %d”, sum); 

  printf(“\nAverage is  %f”,d);

  return 0;

}

Output

Please enter 3 numbers: 4 6 2

Sum is 12

Average is  4.000000

 let’s break down the C program step by step:

  1. #include <stdio.h>: This line includes the standard input-output library, which provides functions like printf and scanf for input and output operations.
  2. int main(): This line marks the beginning of the main function, which is the entry point of a C program. It returns an integer (int) value to the operating system when the program finishes running. In this case, it takes no arguments, indicated by the empty parentheses ().
  3. {: The curly brace marks the beginning of the main function’s body, where the actual code of the program resides.
  4. int a, b, c, sum;: Here, you declare four integer variables: a, b, c, and sum. a, b, and c will be used to store the user’s input numbers, and sum will store their sum.
  5. float d;: You declare a floating-point variable d, which will store the average of the three input numbers.
  6. printf(“Please enter 3 numbers: “);: This line uses the printf function to display the message “Please enter 3 numbers: ” on the console, prompting the user to input three numbers.
  7. scanf(“%d%d%d”, &a, &b, &c);: This line uses the scanf function to read three integer values from the user, which are expected to be entered one after another, separated by spaces. These values are stored in the variables a, b, and c. The & operator is used to provide the memory addresses of these variables.
  8. sum = a + b + c;: Here, you calculate the sum of the three input numbers and store the result in the sum variable.
  9. d = (a + b + c) / 3.0;: This line calculates the average of the three input numbers by adding them and dividing the sum by 3.0 (a floating-point number). The result is stored in the d variable.
  10. printf(“\nSum is %d”, sum);: This line uses printf to display the sum of the three input numbers.
  11. printf(“\nAverage is %f”, d);: Here, you use printf to display the average of the three input numbers.
  12. return 0;: This line indicates the end of the main function and returns an exit status of 0 to the operating system, typically indicating successful program execution.
  13. }: The closing curly brace marks the end of the main function’s body and, thus, the end of the program.

When you run this program:

It prompts you to enter three numbers (in this case, “4 6 2”).

You input three integers separated by spaces and press Enter.

The program calculates the sum of these three numbers (4 + 6 + 2 = 12) and their average (12 / 3.0 = 4.000000).

It then displays the sum and average as “Sum is 12” and “Average is 4.000000,” respectively.

So, in your provided example, when you entered “4 6 2,” the program displayed the result as shown.

Leave a Reply

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