1. 程式人生 > >C 求最大值和最小值問題

C 求最大值和最小值問題

問題:

Write a program that reads in five integers

and then determines and prints the largest and the smallest integers in the group.

#include<stdio.h>
#include<stdlib.h>

int main(void) {
	int max, min, n;
	printf("Input 5 integers:");
	for (int i = 1; i <= 5; i++) {
		scanf_s("%d", &n);

		if (i == 1) {
			max = n;
			min = n;
		}
		else {
			if (max < n) max = n;
			if (min > n) min = n;
		}

	}
	   
	printf("the largest value is %d\n", max);
	printf("the smallest value is %d\n", min);
	system("pause");
	return 0;
}