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

Calculate average

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

 

Suggested for you

Operator in C language

for loop in C language

Single dimensional array in C language

Data type and variable in C language

 

Cpp program to calculate sum of array elements
Cpp Program to calculate average of array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago