I am new to programming and after reading the tutorial for "if else" commands I wrote this small program that checks for the largest number that was put in from the user. It is working, but are there a better way to code this using only variables and if else commands?
#include <iostream>
#include <cstdio>
#include <cstdlib>
usingnamespace std;
int main()
{
int firstNumber;
cout << " Enter the first number:";
cin >> firstNumber;
int secondNumber;
cout << " Enter the second number:";
cin >> secondNumber;
int thirdNumber;
cout << " Enter the third number:";
cin >> thirdNumber;
if (firstNumber > secondNumber && firstNumber > thirdNumber)
{
cout << " The largest number was the first number: " << firstNumber <<endl;
}
elseif (secondNumber > firstNumber && secondNumber > thirdNumber)
{
cout << "The largest number was the second number: " << secondNumber << endl;
}
elseif (thirdNumber > firstNumber && thirdNumber > secondNumber)
{
cout << "The largest number was the third number: " << thirdNumber << endl;
}
system("PAUSE");
return 0;
}
Yes, excluding the loops. It was a practice challenge I read on some other site. I though it would be a good practice to understand how if else can be used.
Thanks for the loop program. I will try to make one on my own later on.