Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using For loop in C
Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using For loop in C++
Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using For loop in Python
Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using For loop in PHP
Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using For loop in Java
Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using For loop in Java Script
Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using For loop in C#
Explanation:
Logic
Convert both numbers to strings to compare their digits individually.
Check if both numbers have the same length. If not, they are not the same.
Use a for loop to iterate through each digit in the strings.
Compare the corresponding digits. If any digit differs, the numbers are not the same.
Program to Print if Numbers Are Same or Not by Comparing Individual Digits using For loop
C
C++
Python
PHP
JAVA
Java Script
C#
#include <stdio.h>
#include <stdbool.h>
// Function to compare digits
bool compare_digits(int num1, int num2) {
while (num1 > 0 && num2 > 0) {
if (num1 % 10 != num2 % 10) {
return false;
}
num1 /= 10;
num2 /= 10;
}
return num1 == 0 && num2 == 0;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
if (compare_digits(num1, num2)) {
printf("The numbers have the same digits.\n");
} else {
printf("The numbers do not have the same digits.\n");
}
return 0;
}
#include <iostream>
using namespace std;
// Function to compare digits
bool compare_digits(int num1, int num2) {
while (num1 > 0 && num2 > 0) {
if (num1 % 10 != num2 % 10) {
return false;
}
num1 /= 10;
num2 /= 10;
}
return num1 == 0 && num2 == 0;
}
int main() {
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
if (compare_digits(num1, num2)) {
cout << "The numbers have the same digits." << endl;
} else {
cout << "The numbers do not have the same digits." << endl;
}
return 0;
}
def compare_digits(num1, num2):
while num1 > 0 and num2 > 0:
if num1 % 10 != num2 % 10:
return False
num1 //= 10
num2 //= 10
return num1 == 0 and num2 == 0
# Input
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if compare_digits(num1, num2):
print("The numbers have the same digits.")
else:
print("The numbers do not have the same digits.")
<?php
function compare_digits($num1, $num2) {
while ($num1 > 0 && $num2 > 0) {
if ($num1 % 10 != $num2 % 10) {
return false;
}
$num1 = (int)($num1 / 10);
$num2 = (int)($num2 / 10);
}
return $num1 == 0 && $num2 == 0;
}
$num1 = (int)readline("Enter the first number: ");
$num2 = (int)readline("Enter the second number: ");
if (compare_digits($num1, $num2)) {
echo "The numbers have the same digits.\n";
} else {
echo "The numbers do not have the same digits.\n";
}
?>
import java.util.Scanner;
public class CompareDigits {
public static boolean compareDigits(int num1, int num2) {
while (num1 > 0 && num2 > 0) {
if (num1 % 10 != num2 % 10) {
return false;
}
num1 /= 10;
num2 /= 10;
}
return num1 == 0 && num2 == 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
if (compareDigits(num1, num2)) {
System.out.println("The numbers have the same digits.");
} else {
System.out.println("The numbers do not have the same digits.");
}
scanner.close();
}
}
function compareDigits(num1, num2) {
while (num1 > 0 && num2 > 0) {
if (num1 % 10 !== num2 % 10) {
return false;
}
num1 = Math.floor(num1 / 10);
num2 = Math.floor(num2 / 10);
}
return num1 === 0 && num2 === 0;
}
let num1 = parseInt(prompt("Enter the first number: "));
let num2 = parseInt(prompt("Enter the second number: "));
if (compareDigits(num1, num2)) {
console.log("The numbers have the same digits.");
} else {
console.log("The numbers do not have the same digits.");
}
using System;
class Program {
static bool CompareDigits(int num1, int num2) {
while (num1 > 0 && num2 > 0) {
if (num1 % 10 != num2 % 10) {
return false;
}
num1 /= 10;
num2 /= 10;
}
return num1 == 0 && num2 == 0;
}
static void Main() {
Console.Write("Enter the first number: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter the second number: ");
int num2 = int.Parse(Console.ReadLine());
if (CompareDigits(num1, num2)) {
Console.WriteLine("The numbers have the same digits.");
} else {
Console.WriteLine("The numbers do not have the same digits.");
}
}
}