C++

C++ Program to Compare Two Numbers1 min read

In this  example, we’ll learn 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.




C++ Code:

C++ Program to Compare Two Numbers
C++ Program to Compare Two Numbers

This code is written in C++ and it compares two numbers entered by the user and prints the result of the comparison.

  1. The code includes the standard input/output library and the cmath library.
  2. In the main function, two variables “num1” and “num2” are declared to store the input numbers.
  3. The program prompts the user to enter two numbers, “num1” and “num2”. The input is stored in the respective variables using the “cin” statement.
  4. An “if-else” statement is used to compare “num1” and “num2”. If “num1” is greater than “num2”, the program prints “Number1 is greater than Number2”.
  5. If “num1” is less than “num2”, the program prints “Number1 is less than Number2”.
  6. If “num1” is equal to “num2”, the program prints “Number1 is equal to Number2”.
  7. The program ends with the return statement.

Leave a Comment