Write a C++ program that selects and displays the maximum value of five numbers to be entered when the program is executed.
(Hint: Use a for loop with both a cin and if statement inside the loop)
1
Expert's answer
2013-03-25T09:24:04-0400
#include <iostream>
using namespace std;
int main() { int number; int maxNumber = -1;
cout << "Enter 5 numbers: "; for (int n = 0; n < 5; n++) { cin >> number; //read next number if (number > maxNumber) //check if it's bigger than maxNumber maxNumber = number; }
/* Print the maximum value */ cout << endl; cout << "The largest number is: " << maxNumber << endl; return 0; }
Comments
Leave a comment