Thursday, 21 June 2018

How to write program to find average of an array of 5 elements

/* Avg of an array */
# include <stdio.h>
void main ()
{
int a[5],i,avg ;
int sum = 0 ;
clrscr () ;
for ( i=o ; i<5 ; i++ )
{
printf ("\n Enter a number:") ;
scanf ("%d"< & a[i]) ;
}
for ( i=0 ; i<5 ; i++ )
sum = sum + a[i] ;
avg = sum / 5 ;
printf ("\n avg= %d", avg) ;
getch () ;
}


DRY RUN ;

sum =0 i=0 0<5
a[0] = 11
i=0 0<5 (TRUE)   i++ = 1
sum = sum + a[i]

similarly
for  i = 1,2,3,4  having true

for i = 5 ( 5<5 ) false

No comments:

Post a Comment

Featured post

Kalgudi Interview Round!!

Those who cleared the second round of kalgudi got an email from exceller to complete their Kalgudi Interview Round. So here we will discuss ...