Program to calculate sum, max, min and average of ‘n’ natural numbers in C

Source Code

// 16. Program to calculate sum, max, min and average of 'n' natural numbers
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
//#include <conio.h>

void main () {
	int num, sum = 0, min, max, i, n;
	float avg;
	//system("cls");
	//clrscr();

	printf("\t\t..:: calculate sum, max, min and average of 'n' natural numbers ::..\n\n");
	printf("Please enter the value of N: ");
	scanf("%d", &n);

	for (i=0; i<n; i++) {
		printf("\nPlease enter number %d: ", i+1);
		scanf("%d", &num);

		if (i==0) min = max = num;
		if (num > max) max = num;
		if (num < min) min = num;

		sum += num;
	}

	avg = sum/(float)n;

	printf("\n\tMax: %d\n\tMin: %d\n\tSum: %d\n\tAvg: %.2f", max, min, sum, avg);
	//getch();
}

Output

		..:: calculate sum, max, min and average of 'n' natural numbers ::..

Please enter the value of N: 5

Please enter number 1: 12

Please enter number 2: 14

Please enter number 3: 32

Please enter number 4: 64

Please enter number 5: 55

	Max: 64
	Min: 12
	Sum: 177
	Avg: 35.40

Leave a Reply

Note: Your email address will not be published. Required fields are marked *