how write a program to calculate the average of 3 numbers and print the result prompt the user to enter 3 values
Answers
Answered by
0
This C program allows the user to enter the number (n) he wish to calculate the average and sum. Next it will ask the user to enter individual item up to declared number. Using the For loop in C Programming it will calculate the sum and later it will calculate the average.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
void main()
{
int i,n,Sum=0,numbers;
float Average;
printf("\nPlease Enter How many Number you want?\n");
scanf("%d",&n);
printf("\nPlease Enter the elements one by one\n");
for(i=0;i<n;++i)
{
scanf("%d",&numbers);
Sum = Sum +numbers;
}
Average = Sum/n;
printf("\nSum of the %d Numbers = %d",n, Sum);
printf("\nAverage of the %d Numbers = %.2f",n, Average);
return 0;
}
OUTPUT:
ANALYSIS:
First printf statement will ask the user to enter n value. For example if the user enters 2 then, second printf statement will ask the user to enter those 2 values one after the other.
For loop will resist the user, not to enter more than 2 values by using the condition i<n.
In the next line we added the entered value to sum.
After completing this it will start the second iteration. For the 3rd iteration the condition (i<n) will fail so, it will exit from the loop.
Outside the loop we calculated the average using the formula sum/n. In our example it is 30/2 = 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
void main()
{
int i,n,Sum=0,numbers;
float Average;
printf("\nPlease Enter How many Number you want?\n");
scanf("%d",&n);
printf("\nPlease Enter the elements one by one\n");
for(i=0;i<n;++i)
{
scanf("%d",&numbers);
Sum = Sum +numbers;
}
Average = Sum/n;
printf("\nSum of the %d Numbers = %d",n, Sum);
printf("\nAverage of the %d Numbers = %.2f",n, Average);
return 0;
}
OUTPUT:
ANALYSIS:
First printf statement will ask the user to enter n value. For example if the user enters 2 then, second printf statement will ask the user to enter those 2 values one after the other.
For loop will resist the user, not to enter more than 2 values by using the condition i<n.
In the next line we added the entered value to sum.
After completing this it will start the second iteration. For the 3rd iteration the condition (i<n) will fail so, it will exit from the loop.
Outside the loop we calculated the average using the formula sum/n. In our example it is 30/2 = 15
Similar questions
English,
7 months ago
Computer Science,
7 months ago
English,
7 months ago
Social Sciences,
1 year ago
Math,
1 year ago
Chemistry,
1 year ago