(Largest and Smallest Integers) Write a program that reads in five integers and determines and prints the largest and the smallest integers in the group. Use only the programming techniques you learned in this chapter.
Solution:
#include <iostream> using std::cin; using std::cout; using std::endl; int main() { int num1, num2, num3, num4, num5; cout << "Enter the five integers: " << endl; cin >> num1; cin >> num2; cin >> num3; cin >> num4; cin >> num5; cout << "The entered integers: " << num1 << ", " << num2 << ", " << num3 << ", " << num4 << ", " << num5 << ".\n"; { if(num1 < num2) if(num1 < num3) if(num1 < num4) if(num1 < num5) cout << "Smallest is " << num1 << endl; if(num1 > num2) if(num2 < num3) if(num2 < num4) if(num2 < num5) cout << "Smallest is " << num2 << endl; if(num3 < num2) if(num3 < num1) if(num3 < num4) if(num3 < num5) cout << "Smallest is " << num3 << endl; if(num5 < num2) if(num5 < num3) if(num5 < num4) if(num5 < num1) cout << "Smallest is " << num4 << endl; if(num4 < num2) if(num4 < num3) if(num4 < num1) if(num4 < num5) cout << "Smallest is " << num5 << endl; } { if(num1 > num2) if(num1 > num3) if(num1 > num4) if(num1 > num5) cout << "Largest is " << num1 << endl; if(num2 > num1) if(num2 > num3) if(num2 > num4) if(num2 > num5) cout << "Largest is " << num2 << endl; if(num3 > num2) if(num3 > num1) if(num3 > num4) if(num3 > num5) cout << "Largest is " << num3 << endl; if(num4 > num2) if(num4 > num3) if(num4 > num1) if(num4 > num5) cout << "Largest is " << num4 << endl; if(num5 > num2) if(num5 > num3) if(num5 > num4) if(num5 > num1) cout << "Largest is " << num5 << endl; } return 0; }
Post A Comment:
0 comments: