-->

Monday, May 19, 2025

C Program to Input elements and to find average value using array-1D array concept

 

C program to input elements and to find average 


//array input and printing average value

#include<stdio.h>

int main()

{

int a[5];//array declaration

int i,sum=0;//variable declaration

float average;//float to store average

for(i=0;i<=4;i++)

{

printf(" enter value for location=%d\n",i+1);

scanf("%d",&a[i]);//input for given array

sum+=a[i]; //finding sum

}

average=(float)sum/5; //type casting and storing average in variable

printf("the average value is=%f",average);//printng result

return 0;

}

Output:-

C program to find average of five elements using array



second method:

//array input and printing average value. Here, we will use define for size.

#include<stdio.h>
#define size 5
int main()
{
int a[size];//array declaration with size as an index value. It takes the value from define
int i,sum=0;//variable declaration
float average;//float to store average
for(i=0;i<=4;i++)
{
printf(" enter value for location=%d\n",i+1);
scanf("%d",&a[i]);//input for given array
sum+=a[i]; //finding sum
}
average=(float)sum/5; //type casting and storing average in variable
printf("the average value is=%f",average);//printng result
return 0;
}