In this tutorial, we will discuss a simple concept of C Program to calculate the average of an array
In this topic, we will learn about how to calculate the average of n number of elements (integer or floating point)using the array in C programming language.
Average calculates using the operator in C programming language
Program 1
In this program, we can see what should be step by step procedure for completion of the program.
#include <stdio.h> #include <stdlib.h> int main() { int arr[]={56,78,94,56,73,42,31,67}; //Array declaration and initialization int total=0,i;//variable declaration for(i=0; i<=7; i++){ total=total+arr[i];//loop for calculatin total } double avg=total/i;//calculate average printf("The average is:%.2f ",avg); getch();//display result on the screen return 0; }
When the above code is compiled and executed, it produces the following results
The average is: 62.125
Program 2
In this program, we can see step by step procedure for completion of the program.
#include <stdio.h> #include <stdlib.h> int main() { int arr[20],num,i;//array declaration double avg=0,sum=0;//variable declaration printf("Enter the numbers of average: "); scanf("%d",&num);;;get inpur from user to numberof elements printf("Enter the numbers: \n"); for(i=1; i<=num; i++){//loop for get input numbers scanf("%d", &arr[i]); } for(i=1; i<=num; i++){ sum=sum+arr[i];//loop for calculating sum avg=sum/num;//calculate average } printf("Average of entered numbers are: %f",avg); getch();//display result on the screen return 0; }
When the above code is compiled and executed, it produces the following results
Enter the numbers of average: 5 Enter the numbers: 67 87 98 90 89 Average of entered numbers are: 86.200000
Program 3
In this program, we can see what should be step by step procedure for completion of the program.
#include <stdio.h> #include <stdlib.h> int main() { float arr[5]={23.45,25.4,38.99,45.6,12.5}; //Array declaration and initialization float total=0.0; float avg=0.0;//variable declaration int i; for(i=0; i<=4; i++){ total=total+arr[i];//loop for calculate total } avg=total/5; printf("The average is:%.2f ",avg); getch(); return 0; }
When the above code is compiled and executed, it produces the following results
The average is: 29.19
Program 4
In this program, we can see what should be step by step procedure for completion of the program.
#include <stdio.h> #include <stdlib.h> int main() { float arr[20];//array declaration int i,num;//variable declaration double avg=0.0,sum=0.0; printf("Enter the numbers of average: "); scanf("%d",&num);//get input from user for(i=0; i<num; i++){ //loop for get input from user printf("Enter the number %d: ",i+1); scanf("%f", &arr[i]); } for(i=0; i<num; i++){ //loop calculating sum of array elements sum=sum+arr[i]; avg=sum/num; //calculating average } printf("Average of entered numbers are: %.2f",avg); getch();//display result on the screen return 0; }
When the above code is compiled and executed, it produces the following results
Enter the number of average: 5 Enter the number 1: 12.23 Enter the number 2: 23.34 Enter the number 3: 34.45 Enter the number 4: 45.56 Enter the number 5: 56.67 Average of entered numbers are: 34.45
Similar post
Java Program to calculate average of array
C Program to calculate average of array
Java Program to calculate average of odd and even numbers in an array
Java Program to calculate average of odd and even numbers
C Program to calculate average of odd and even numbers in an array
C Program to calculate average of odd and even numbers
C++ Program to calculate average of odd and even numbers in an array
C++ Program to calculate average of odd and even numbers
Suggested for you
Single dimensional array in C language
Data type and variable in C language
Single dimensional array in C++ language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…
View Comments
write #include in your program or else getch() won't work.