C Program to calculate the average of an array
- Home
- Calculations
- C Program to calculate the average of an array
- On
- By
- 0 Comment
- Categories: Calculations
C Program to calculate the average of an array
C Program to calculate the average of an array
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
find the average of integer values
Program 1
In this program, we can see what should be step by step procedure for completion of the program.
- Array declaration and initialization
- Essential variable declaration
- loop for calculating total through each value
- Calculating average
- Display result on the screen
Calculate the average of numbers using an array
#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.
- Array declaration
- Essential variable declaration
- loop for taking input from the user for every index
- loop for calculating total through each value
- Calculating average
- Display result on the screen
Calculate the average of numbers using an array – entered by the user
#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
find the average of floating point values
Program 3
In this program, we can see what should be step by step procedure for completion of the program.
- Array declaration and initialization
- Essential variable declaration
- Loop for calculating total through each value
- Calculating average
- Display result on the screen
Calculate the average of numbers using an array
#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.
- Array declaration
- Essential variable declaration
- loop for taking input from the user for every index
- loop for calculating total through each value
- Calculating average
- Display result on the screen
Calculate the average of numbers using an array – entered by the user
#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
No Comments
yhhkjml; March 3, 2022 at 6:40 am
write #include in your program or else getch() won’t work.