In this article, we will write a C program to check if two numbers are equal or not.
The program takes two numbers from the user as input and checks if both numbers are equal or not.
Sample Input:
Enter First Number: 10
Enter Second Number: 20
Output:
Numbers are not equal
To check if two numbers are equal or not we can use the equality operator(==
). It is a binary operator that compares two values and returns true if both values are equal and false if they are not.
The following C program shows how you can check if two numbers are equal or not:
// C program to check if two numbers are equal #include <stdio.h> int main() { int num1, num2; printf("Enter First Number: "); scanf("%d", &num1); printf("Enter Second Number: "); scanf("%d", &num2); if (num1 == num2) { printf("Both numbers are equal."); } else { printf("Numbers are not equal"); } return 0; }
Output:
Enter First Number: 10 Enter Second Number: 20 Numbers are not equal
Code Explanation:
- The program asks the user to enter two numbers and stores them in
num1
andnum2
variables respectively. - To check if both numbers are equal or not we used the equality operator. It returns true if both numbers are equal and false if they are not.
- If both numbers are equal, the if block’s code is executed, otherwise, the else block’s code is executed.
- Finally, the message is printed on the screen using the printf() function.
Thanks for reading.