# Comparing two Numbers in C++

Here we are going to look at an example which is able of comparing two numbers in C++. In future we might need a simple code for comparing two integers to be included in our larger codes. Here we will use the “if” conditional structure, and relational operators .So look at this example for comparing two numbersin C++:

1. //ComparingÂ twoÂ numbers
2. //UsingÂ ifÂ statements,Â relationalÂ operators
3. #includeÂ <iostream>
4. usingÂ namespaceÂ std;
5. intÂ main()
6. {
7. Â Â Â Â intÂ num1;
8. Â Â Â Â intÂ num2;
9. Â
10. Â Â Â Â coutÂ <<Â “EnterÂ twoÂ integers,Â andÂ IÂ willÂ tellÂ youn”
11. Â Â Â Â <<Â “theÂ relationshipsÂ theyÂ satisfy:Â “;
12. Â Â Â Â cinÂ >>Â num1Â >>Â num2;Â Â Â //Â getÂ twoÂ integers
13. Â
14. Â Â Â Â //comparingÂ twoÂ numbersÂ startsÂ here
15. Â Â Â Â ifÂ (Â num1Â ==Â num2Â )
16. Â Â Â Â Â Â Â Â coutÂ <<Â num1Â <<Â ”Â isÂ equalÂ toÂ “Â <<Â num2Â <<Â endl;
17. Â
18. Â Â Â Â ifÂ (Â num1Â !=Â num2Â )
19. Â Â Â Â Â Â Â Â coutÂ <<Â num1Â <<Â ”Â isÂ notÂ equalÂ toÂ “Â <<Â num2Â <<Â endl;
20. Â
21. Â Â Â Â ifÂ (Â num1Â <Â num2Â )
22. Â Â Â Â Â Â Â Â coutÂ <<Â num1Â <<Â ”Â isÂ lessÂ thanÂ “Â <<Â num2Â <<Â endl;
23. Â
24. Â Â Â Â ifÂ (Â num1Â >Â num2Â )
25. Â Â Â Â Â Â Â Â coutÂ <<Â num1Â <<Â ”Â isÂ greaterÂ thanÂ “Â <<Â num2Â <<Â endl;
26. Â
27. Â Â Â Â ifÂ (Â num1Â <=Â num2Â )
28. Â Â Â Â Â Â Â Â coutÂ <<Â num1Â <<Â ”Â isÂ lessÂ thanÂ orÂ equalÂ toÂ “<<Â num2Â <<Â endl;
29. Â
30. Â Â Â Â ifÂ (Â num1Â >=Â num2Â )
31. Â Â Â Â Â Â Â Â coutÂ <<Â num1Â <<Â ”Â isÂ greaterÂ thanÂ orÂ equalÂ toÂ “<<Â num2Â <<Â endl;
32. Â
33. Â Â Â Â returnÂ 0;
34. }

Let’s explain the code now. first of all we took two variables of datatype “int”, then we asked the user to enter the two integers, then we started the code for comparing two numbers, as we need comparing two numbers so first of all we used “if” structure for “equal”, then for “not equal”, then for “less than”, then “greater than”, then “less than or equal to”, and at the end for “greater than or equal to”. Now while executing the program the condition or conditions that are true will be executed and we will get our answer after comparison. So comparing two numbers in C++ is not so difficult.

#### Latest Content

Android Tutorial For Beginners

### Android Webview Tutorial With Example | How to Use Webview in Android

College Assignments

### Student Registration Project in PHP

College Assignments

PHP

C / C++