In this tutorial, we are going to learn a writing program in the C Programming language to find the Average of numbers.
This is How our program will behave
1). At first Our C Program will take the total count for the numbers for which we want to calculate the average.
2). After taking the count, We have to give the number to calculate the average.
3). Here we are considering input value as Integer and output may be an integer or float value.
What is the average number?
Average is a mathematical calculation that can be obtained by the sum of numbers divided by the total count of the numbers.
For example
Suppose we want to calculate the Average of 1,2,3,4,5
So, first, find the sum of all numbers
1+2+3+4+5 = 15
And here total count for the numbers is 5.
So Average will be 15/5 = 3
C Program to Calculate the average of numbers using for loop
#include<stdio.h>
void main() {
int n, i;
float num, average, sum = 0;
printf("=== C Program to Calculate the average of numbers === \n");
printf("How many numbers you have to Calculate average?:");
scanf("%d", & n);
for (i = 1; i <= n; i++) {
printf("Enter the %d number: ", i);
scanf("%f", & num);
sum += num;
}
average = sum / n;
printf("\nTotal Average is = %1.2f\n ", average);
}
Output
=== C Program to Calculate the average of numbers ===
How many numbers you have to Calculate average?:5
Enter the 1 number: 3
Enter the 2 number: 4
Enter the 3 number: 2
Enter the 4 number: 1
Enter the 5 number: 7
Total Average is = 3.40
C Program to calculate the average of numbers Using while loop
#include<stdio.h>
#include<conio.h>
void main() {
int n, i = 1;
float num, average, sum = 0;
printf("=== C Program to Calculate the average of numbers === \n");
printf("Enter the value of n:");
scanf("%d", &n);
while (i <= n) {
printf("Enter the %d number: ", i);
scanf("%f", &num);
sum += num;
i++;
}
average = sum / n;
printf("\nTotal Average is = %1.2f\n ", average);
}
Output:
=== C Program to Calculate the average of numbers ===
Enter the value of n:6
Enter the 1 number: 43
Enter the 2 number: 44
Enter the 3 number: 21
Enter the 4 number: 22
Enter the 5 number: 78
Enter the 6 number: 2
Total Average is = 35.00
What did you think?
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…