01. Make a program that asks for two numbers, and say which one is greater, witch is smaller or if they are equal.
How to compare two numbers in C++
Let's store the numbers in variables num1 and num2.First, let's test if the first one is larger than the second, if it go in the first IF and says it in cout.
If not, fall into the ELSE.
In this ELSE, the second number is greater than the first, or can be equal.
We tested this with a new nested IF, to see if num2 is greater than num1, if so, we warn you.
If num1 is not greater than num2, or num2 is not greater than num1, then it falls into the nested ELSE and we necessarily have the numbers to be equal.
See how our code looked:
#include <iostream> using namespace std; int main() { int num1, num2; cout << "Number 1: "; cin >> num1; cout << "Number 2: "; cin >> num2; if(num1>num2) cout <<num1<<" is greater than "<<num2<<endl; else if(num2>num1) cout <<num2<<" is greater than "<<num1<<endl; else cout << "Equals"<<endl; return 0; }
No comments:
Post a Comment