C program Sum and Average of N numbers
It has two C program to do sum and average of N numbers.
- Sum of N numbers - N integers are given to it as input and return sum of input integers as result
- Average of N numbers - N integers are given to it as input and return average of inputs integers as result
Sum of N numbers - algorithm
The input N integers read from user add the N integers store result into a variable sum. and then prints sum value after end of loop.
set sum=0 Read N integers For i=1 to N sum = sum + integers[i] End For print sum
Average of N numbers - algorithm
The input N integers read from user add the N integers store result into a variable sum. after end of loop, divide sum value by N and store results into a variable average, then prints average value .
set sum=0,avg=0 Read N integers For i=1 to N sum = sum + integers[i] End For avg = sum / N print avg
Sum of N numbers -C programming code
#include <stdio.h>
#include <stdlib.h>
int main() {
int N;
printf ("\n Find sum of N numbers ");
printf("\n Enter value for N:" );
scanf("%d", &N);
int *val=(int*) malloc(sizeof(int)*N);
int sum=0;
for( int i=0;i< N ; i++ ) {
printf("\n Enter %dth value :" ,i);
scanf("%d", &val[i]);
sum = sum + val[i] ;
}
printf("\nThe sum of N numbers is =%d",sum);
return 0;
}
Sum of N numbers -C program Output
Find sum of N numbers Enter value for N:5 Enter 0th value :10 Enter 1th value :20 Enter 2th value :32 Enter 3th value :21 Enter 4th value :32 The sum of N numbers is =115
Average of N numbers -C programming code
#include <stdio.h>
#include <stdlib.h>
int main() {
int N;
printf ("\n Find average of N numbers ");
printf("\n Enter value for N:" );
scanf("%d", &N);
int *val=(int*) malloc(sizeof(int)*N);
float sum=0;
for( int i=0;i< N ; i++ ) {
printf("\n Enter %dth value :" ,i );
scanf("%d", &val[i]);
sum = sum + val[i] ;
}
float avg = sum/N;
printf("\nThe average of N numbers is = %.2f",avg);
return 0;
}
Average of N numbers -C program Output
Find average of N numbers Enter value for N:5 Enter 0th value :6 Enter 1th value :8 Enter 2th value :2 Enter 3th value :5 Enter 4th value :2 The average of N numbers is = 4.60
Comments
Post a Comment