Find Minimum and Maximum Number in an Array with C++.

Finding the minimum and maximum in an array is a classical programming exercise in C++. Here’s how is done!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
//finding min and max in array
int main()
{
	int num[5] = { 2, 50, 100, 25, 78 };
	int max = num[0], min = num[0]; //set max & min to some value
	for(int i = 0; i < 5; i++)//loop through array
	{
		if(num[i] > max) { max = num[i]; } //find max
		if(num[i] < min) { min = num[i]; } //find min
	}
	cout << "Maximum number: " << max << endl;
	cout << "Minimum number: " << min << endl;
 
	system("pause");
	return 0;
}
Share This!

One thought on “Find Minimum and Maximum Number in an Array with C++.

Leave a Reply

Your email address will not be published.