equal or not in C
Table of Contents
Concept
the concept for checking whether two numbers are equal or not in C:
- Declare two integer variables to store the user-entered numbers, let’s call them
num1
andnum2
. - Use
scanf
function to read the first number from the user and store it innum1
. - Use
scanf
function again to read the second number from the user and store it innum2
. - Use an if statement to compare the values of
num1
andnum2
. - In the if statement condition, use the equality operator
==
to check ifnum1
is equal tonum2
. - If the condition is true, execute a code block indicating that the numbers are equal. For example, you can print a message like “The numbers are equal.”
- If the condition is false, execute a different code block indicating that the numbers are not equal. For example, you can print a message like “The numbers are not equal.”
the concept for checking whether two numbers are equal or not in C++:
- Declare two variables to store the user-entered numbers, let’s call them
num1
andnum2
. - Use
std::cin
to read the first number from the user and store it innum1
. - Use
std::cin
again to read the second number from the user and store it innum2
. - Use an if statement to compare the values of
num1
andnum2
. - In the if statement condition, use the equality operator
==
to check ifnum1
is equal tonum2
. - If the condition is true, execute a code block indicating that the numbers are equal. For example, you can use
std::cout
to print a message like “The numbers are equal.”. - If the condition is false, execute a different code block indicating that the numbers are not equal. For example, you can use
std::cout
to print a message like “The numbers are not equal.”.
the concept for checking whether two numbers are equal or not in Java:
- Declare two variables to store the user-entered numbers, let’s call them
num1
andnum2
. - Use the
Scanner
class to read the first number from the user and store it innum1
. - Use the
Scanner
class again to read the second number from the user and store it innum2
. - Use an if statement to compare the values of
num1
andnum2
. - In the if statement condition, use the equality operator
==
to check ifnum1
is equal tonum2
. - If the condition is true, execute a code block indicating that the numbers are equal. For example, you can use
System.out.println()
to print a message like “The numbers are equal.”. - If the condition is false, execute a different code block indicating that the numbers are not equal. For example, you can use
System.out.println()
to print a message like “The numbers are not equal.”.
the concept for checking whether two numbers are equal or not in Python:
- Prompt the user to enter the first number and store it in a variable, let’s call it
num1
. - Prompt the user to enter the second number and store it in a variable, let’s call it
num2
. - Use an if statement to compare the values of
num1
andnum2
. - In the if statement condition, use the equality operator
==
to check ifnum1
is equal tonum2
. - If the condition is true, execute a code block indicating that the numbers are equal. For example, you can use the
print()
function to display a message like “The numbers are equal.”. - If the condition is false, execute a different code block indicating that the numbers are not equal. For example, you can use the
print()
function to display a message like “The numbers are not equal.”.
C
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
if (num1 == num2) {
printf("The numbers are equal.\n");
} else {
printf("The numbers are not equal.\n");
}
return 0;
}
In both the C and C++ programs, the user is prompted to enter two numbers. The program then compares the two numbers using the equality operator ==
. If the numbers are equal, it displays the message “The numbers are equal.” Otherwise, it displays the message “The numbers are not equal.”
C++
#include <iostream>
int main() {
int num1, num2;
std::cout << "Enter the first number: ";
std::cin >> num1;
std::cout << "Enter the second number: ";
std::cin >> num2;
if (num1 == num2) {
std::cout << "The numbers are equal." << std::endl;
} else {
std::cout << "The numbers are not equal." << std::endl;
}
return 0;
}
Python
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if num1 == num2:
print("The numbers are equal.")
else:
print("The numbers are not equal.")
In this Python program, the user is prompted to enter two numbers using the input()
function. The float()
function is used to convert the input to floating-point numbers for numerical comparison.
The program then uses the equality operator ==
to check if the two numbers are equal. If they are equal, it prints the message “The numbers are equal.” Otherwise, it prints the message “The numbers are not equal.”
Java
import java.util.Scanner;
public class NumberEqualityChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
if (num1 == num2) {
System.out.println("The numbers are equal.");
} else {
System.out.println("The numbers are not equal.");
}
}
}
javaScript
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter the first number: ", function(num1) {
rl.question("Enter the second number: ", function(num2) {
if (parseFloat(num1) === parseFloat(num2)) {
console.log("The numbers are equal.");
} else {
console.log("The numbers are not equal.");
}
rl.close();
});
});
In this JavaScript program, the user is prompted to enter two numbers using the readline
module. The parseFloat()
function is used to convert the input to floating-point numbers for numerical comparison.
The program then uses the equality operator ===
to check if the two numbers are equal. If they are equal, it prints the message “The numbers are equal.” Otherwise, it prints the message “The numbers are not equal.”