In this C++ Example, I’ll show how to compare two numbers using if else if statements.
We use the following operators for comparison in C #. In this example we will perform a simple comparison process.
Compare Two Numbers
Variables are defined in the first row. In the next lines, values are assigned to these variables. Then Numbers compare in if statement.
In the last line we print the result of processing on the screen.
Source Code:
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 |
#include <iostream> #include<stdlib.h> using namespace std; int main() { int num1,num2; cout<<"Number 1: "; cin>>num1; cout<<"Number 2: "; cin>>num2; if (num1 > num2) { cout<<"Number1 is greater than Number2"; } else if (num1 < num2) { cout<<"Number1 is less than Number2"; } else { cout<<"Number1 is equal to Number2"; } } |
Output:
You can find more similar examples of programming for this programming language in the site.