Program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop

Program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop

  • Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop in C
  • Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop in C++
  • Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop in Python
  • Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop in PHP
  • Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop in Java
  • Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop in Java Script
  • Write a program to Print if Numbers Are Same or Not by Comparing Individual Digits using While loop in C#

Explanation:

Logic

  1. Convert both numbers to strings to compare their digits individually.
  2. Check if both numbers have the same length. If not, they are not the same.
  3. Use a for loop to iterate through each digit in the strings.
  4. 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 While loop

#include <stdio.h>

int compareDigits(int num1, int num2) {
    while (num1 > 0 && num2 > 0) {
        if (num1 % 10 != num2 % 10)
            return 0;
        num1 /= 10;
        num2 /= 10;
    }
    return num1 == 0 && num2 == 0;
}

int main() {
    int num1, num2;
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    if (compareDigits(num1, num2))
        printf("Digits of both numbers are the same.\n");
    else
        printf("Digits of both numbers are not the same.\n");

    return 0;
}

#include <iostream>
using namespace std;

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;
}

int main() {
    int num1, num2;
    cout << "Enter two numbers: ";
    cin >> num1 >> num2;

    if (compareDigits(num1, num2))
        cout << "Digits of both numbers are the same." << endl;
    else
        cout << "Digits of both numbers are not the same." << 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

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

if compare_digits(num1, num2):
    print("Digits of both numbers are the same.")
else:
    print("Digits of both numbers are not the same.")

<?php
function compareDigits($num1, $num2) {
    while ($num1 > 0 && $num2 > 0) {
        if ($num1 % 10 != $num2 % 10) {
            return false;
        }
        $num1 = intdiv($num1, 10);
        $num2 = intdiv($num2, 10);
    }
    return $num1 == 0 && $num2 == 0;
}

$num1 = intval(readline("Enter the first number: "));
$num2 = intval(readline("Enter the second number: "));

if (compareDigits($num1, $num2)) {
    echo "Digits of both numbers are the same.\n";
} else {
    echo "Digits of both numbers are not the same.\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("Digits of both numbers are the same.");
        } else {
            System.out.println("Digits of both numbers are not the same.");
        }
    }
}

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;
}

const num1 = parseInt(prompt("Enter the first number:"));
const num2 = parseInt(prompt("Enter the second number:"));

if (compareDigits(num1, num2)) {
    console.log("Digits of both numbers are the same.");
} else {
    console.log("Digits of both numbers are not the same.");
}

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(string[] args) {
        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("Digits of both numbers are the same.");
        } else {
            Console.WriteLine("Digits of both numbers are not the same.");
        }
    }
}

List of All Programs