google.com, pub-6167773875660516, DIRECT, f08c47fec0942fa0 Write a program to find the sum and average of n numbers of the array - 2nd puc computer science

Write a program to find the sum and average of n numbers of the array

Write a program to find the sum and average of n numbers of the array

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],n,i,sum=0;

float average;

clrscr();

printf("How many elements? ");

scanf("%d",&n);

printf("Enter the elements\n");

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

scanf("%d",&a[i]);

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

sum+=a[i]; // shorthand assignment operator which is sum=sum+a[i]

average=(float)sum/n; // explicit type conversion

printf("Sum=%d\n",sum);

printf("Average=%f",average);

getch();

}

Output:

How many elements? 3

Enter the elements                                                             

1                                                                              

2                                                                              

3                                                                               

Sum=6                                                                          

Average=2.000000
Write a program to find the sum and average of n numbers of the array Write a program to find the sum and average of n numbers of the array Reviewed by Vision Academy on December 12, 2021 Rating: 5

No comments:

CheckOut

Powered by Blogger.