#include <stdio.h> int main() { int n, i; float num[100], sum = 0.0, avg; printf("Enter the numbers of elements: "); scanf("%d", &n); while (n > 100 || n < 1) { printf("Error! number should in range of (1 to 100).\n"); printf("Enter the number again: "); scanf("%d", &n); } for (i = 0; i < n; ++i) { printf("%d. Enter number: ", i + 1); scanf("%f", &num[i]); sum += num[i]; } avg = sum / n; printf("Average = %.2f", avg); return 0; } Output Show Here, the user is first asked to enter the number of elements. This number is assigned to n. If the user entered integer is greater less than 1 or greater than 100, the user is asked to enter the number again. This is done using a while loop. Then, we have iterated a for loop from i = 0 to i . In each iteration of the loop, the user is asked to enter numbers to calculate the average. These numbers are stored in the num[] array. scanf("%f", &num[i]);And, the sum of each entered element is computed. sum += num[i];Once the for loop is completed, the average is calculated and printed on the screen. April 4, 2021 | C Programming Example | |In this C Programming example, we will implement the program to find the average of two numbers and print the output on the console. 1. How to find the Average of two numbers?The average of two numbers can be calculated using two methods.
Helpful topics to understand this program better are-
2. C Program to find the average of two numbersLet’s discuss the execution(kind of pseudocode) for the program to find the average of two numbers in C. 2.1. Standard Method
Let us now implement the above execution of the program to find the average of two numbers in C. #include <stdio.h> int main(){ int number1, number2; float average; printf("Enter the first number: "); scanf("%d",&number1); printf("Enter the second number: "); scanf("%d",&number2); average= (float)(number1 + number2)/2; printf("The Average of %d and %d is: %.2f",number1,number2,average); return 0; }Note: In the above program, %.2f is used to display floating point number up to two decimal places. Output Enter the first number: 1 Enter the second number: 3 The Average of 1 and 3 is: 2.002.2. Using Function
Note: In the above program, %.2f is used to display floating point number up to two decimal places. Output Enter the first number: 5 Enter the second number: 4 The Average of 5 and 4 is: 4.503. ConclusionIn this C Programming example, we have discussed how to find the average of two numbers in C. Helpful LinksPlease follow C Programming tutorials or the menu in the sidebar for the complete tutorial series. Also for the example C programs please refer to C Programming Examples. Recommended BooksAn investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding Do not forget to share and Subscribe. Happy coding!! 😊 Recommended -C Program to find the average of two numbers was last modified: April 4th, 2021 by Would love your thoughts, please comment.x
To find the average of two numbers in C programming, find their sum using C Addition Operator and divide the sum with 2 (since there are only two numbers) using C Division Operator.
C ProgramIn the following program, we read two numbers into n1 and n2 from user, and find their average using the formula (n1 + n2) / 2. main.c #include <stdio.h> int main() { int n1, n2; float avg = 0; printf("Enter n1 : "); scanf("%d", &n1); printf("Enter n2 : "); scanf("%d", &n2); avg = (float)(n1 + n2) / 2; printf("Average : %f\n", avg); return 0; }Output Enter n1 : 2 Enter n2 : 3 Average : 2.500000 Program ended with exit code: 0Output Enter n1 : 10 Enter n2 : 20 Average : 15.000000 Program ended with exit code: 0ConclusionIn this C Tutorial, we learned how to find the average of two numbers using C program. Here we will write two C programs to find the average of two numbers(entered by user). Example 1: Program to find the average of two numbers#include <stdio.h> int main() { int num1, num2; float avg; printf("Enter first number: "); scanf("%d",&num1); printf("Enter second number: "); scanf("%d",&num2); avg= (float)(num1+num2)/2; //%.2f is used for displaying output upto two decimal places printf("Average of %d and %d is: %.2f",num1,num2,avg); return 0; }Output: Enter first number: 12 Enter second number: 13 Average of 12 and 13 is: 12.50Example 2: Program to find the average using functionIn this program, we have created a user defined function average() for the calculation of average. The numbers entered by user are passed to this function during function call. #include <stdio.h> float average(int a, int b){ return (float)(a+b)/2; } int main() { int num1, num2; float avg; printf("Enter first number: "); scanf("%d",&num1); printf("Enter second number: "); scanf("%d",&num2); avg = average(num1, num2); //%.2f is used for displaying output upto two decimal places printf("Average of %d and %d is: %.2f",num1,num2,avg); return 0; }Output: Enter first number: 20 Enter second number: 13 Average of 20 and 13 is: 16.50Check out the related C Programs: |