**C program to find average of n numbers using an array** is to find average.

Find average is a program to find an average of total numbers in the array.

In this tutorial we are going to see a simple program which is to find an average, What is average, an average program in c.

**What is average **

The mean is the average. When you add all the number and divide sum by a number of total numbers the resultant value is referred to as average.

**Average formula :**

The average = sum of all numbers / total numbers

**The logic to find an average **

First, we take a total number of input from the user. Then we take numbers from the user. Next, we calculate the average by using formula and print the average of numbers.

**Algorithm to find average **

- Start
- Declare variables
- Take a input total number from user
- Take a numbers input from user
- Calculate average using formula
- Print average of numbers
- End

## C program to find average of N numbers using array

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> main() { int i,j,sum=0,num; float avg; printf("Enter number\n"); scanf("%d",&num); int array[num]; printf("Enter %d numbers\n",num); for(i=0;i<num;i++) { scanf("%d",&array[i]); sum+=array[i]; } avg=(float)sum/num; printf("Average of %d numbers is %f\n",num,avg); } |

**Output :**

**Explanation :**

**1. First we include the preprocessor directives in c.**

**2. After that write the main function.**

**3. Declare the variables**

i, j => temporary variable

num => to store total numbers

array=> is array to store n numbers

sum => to store sum

avg => to store average

**4. Take a input total number of students in class**

printf(“Enter number\n”);

scanf(“%d”,&num);

=> example :- num = 4

**5. Next take a numbers **

printf(“Enter %d numbers \n”,num);

for(i=0;i<num;i++) => to take a 4 numbers from user

**At iteration 1 :**

i = 0, 0 < 4 this condition becomes true for loop executed

scanf(“%d”,&array[i]) => array[0] = 80

sum+=array[i] => sum = 80

**At iteration 2 :**

i = 1, 1 < 4 this condition becomes true for loop executed

scanf(“%d”,&array[i]) => array[1] = 80

sum+=array[i] => sum = 160

**At iteration 3 :**

i = 2, 2 < 4 this condition becomes true for loop executed

scanf(“%d”,&array[i]) => array[2] = 90

sum+=array[i] => sum = 250

**At iteration 4 :**

i = 3, 3 < 4 this condition becomes true for loop executed

scanf(“%d”,&array[i]) => array[3] = 90

sum+=array[i] => sum = 340

**At iteration 5 :**

i = 4, 4 < 4 this condition becomes false and for loop execution stopped

Next, calculate the average using the formula

Average = sum of total numbers / total numbers

We calculated the sum of the numbers

sum = 340

avg=(float)sum/num => avg = 85

Next print the average of numbers

printf(“Average of %d number is %f\n”,num,avg)

=> Average of 5 number is 85.00

**Recommended Posts **

## Leave a Reply