Output largest and smallest number out of four numbers
Oct 5, 2016 at 4:31pm UTC
So I was challenged in class to write a code that lets the user enter in four numbers. The code is supposed to output the largest and smallest number, which I have done, but i have to do it in only four if statements and my code that I have written does it with five and else if statements count as a if statement.
Please help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <cstdlib>
#include <iostream>
using namespace std;
/*
*
*/
int main(int argc, char ** argv) {
int w, x, y, z, smallest, biggest;
cout << "You are going to enter in 4 numbers" << endl << endl;
cout << "Enter the first number: " ;
cin >> w;
cout << "Enter the second number: " ;
cin >> x;
cout << "Enter the third number: " ;
cin >> y;
cout << "Enter the fourth number: " ;
cin >> z;
cout << endl;
if (w > x){
biggest = w;
smallest = x;
}
else
biggest = x;
smallest = w;
if (y > biggest)
biggest = y;
else if (y < smallest)
smallest = y;
if (z > biggest)
biggest = z;
else if (z < smallest)
smallest = z;
cout << "The biggest number was: " << biggest << endl;
cout << "The smallest number was: " << smallest << endl;
cout << endl << endl;
return 0;
}
Oct 5, 2016 at 4:41pm UTC
I have just edited my code and I think I solved my own problem!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <cstdlib>
#include <iostream>
using namespace std;
/*
*
*/
int main(int argc, char ** argv) {
int w, x, y, z, smallest, biggest, smallest2, biggest2;
cout << "You are going to enter in 4 numbers" << endl << endl;
cout << "Enter the first number: " ;
cin >> w;
cout << "Enter the second number: " ;
cin >> x;
cout << "Enter the third number: " ;
cin >> y;
cout << "Enter the fourth number: " ;
cin >> z;
cout << endl;
if (w > x){
biggest = w;
smallest = x;
}
else
biggest = x;
smallest = w;
if (y > z){
biggest2 = y;
smallest2 = z;
}
else
smallest2 = y;
biggest2 = z;
if (biggest2 > biggest)
biggest = biggest2;
else
biggest = biggest;
if (smallest2 < smallest)
smallest = smallest2;
else
smallest = smallest;
cout << "The biggest number was: " << biggest << endl;
cout << "The smallest number was: " << smallest << endl;
cout << endl << endl;
return 0;
}
It might be a little confusing to read since I chose to use the words biggest and biggest2 along with smallest and smallest2 but there you go!
Topic archived. No new replies allowed.