Coding Practice: Write a program in C++ to find out maximum and minimum number from given integer numbers using conditional statement if()
Problem: Write a program in C++ to to find out maximum and minimum number from given four integer numbers using conditional statement if() and display the result on computer screen display
This program demonstrates usage of conditional statement "if else" in C++ language".
So, let's do it --
/*This program demonstrates how to find out maximum and minimum number from given four integer numbers using conditional statement if() in C++ language */
//6530
//590
//28640
//32
//Programmed by Phantom
#include <iostream> // include standard input output (I/O) stream
using namespace std;
int main() //main function
{
int number1, number2, number3, number4; //local variable declaration
int maximum, minimum;
// assign values to all integer variables
number1 = 6530;
number2 = 590;
number3 = 28640;
number4 = 32;
//find out maximum number
if(number1 > number2)
{
maximum = number1;
}
else
{
maximum = number2;
}
if(maximum < number3)
{
maximum = number3;
}
if(maximum < number4)
{
maximum = number4;
}
//print the maximum number on the computer screen
cout << "Maximum number is " << maximum << " in the given numbers " << number1 << " " << number2 << " " << number3 << " " << number4 << endl;
//find out minimum number
if(number1 < number2)
{
minimum = number1;
}
else
{
minimum = number2;
}
if(minimum > number3)
{
minimum = number3;
}
if(minimum > number4)
{
minimum = number4;
}
//print the minimum number on the computer screen
cout << "Minimum number is " << minimum << " in the given numbers " << number1 << " " << number2 << " " << number3 << " " << number4 << endl;
return 0; //program executed successfully
} //End of program
Output
Maximum number is 28640 in the given numbers 6530 590 28640 32
Minimum number is 32 in the given numbers 6530 590 28640 32
Do it yourself on online C++ compiler: https://www.tutorialspoint.com/compile_cpp_online.php
Or, download Turbo C++ (ver. 3.0)
Download Turbo C++ for Windows 7, 8, 8.1 and Windows 10 (32-64 bit) with full/window screen mode and many more extra feature
Tomorrow I'll post another C++ problem with solution.
Have a nice day.
Thank you very much.
Generalize the problem. The way you did it, it is feasable for very few numbers: I'd say 2 as top limit (where you can use a max(a, b) function or macro), so you are beyond my threshold by 2 already. Consider that in real life you won't ever have this kind of problem, and if you do, you are not going to make a sequence of ifs.
thanks for your valuable comment:)
This program is only for new programmer in C++