Program to Check If Given Number is Palindrome or Not using While loop

Program to Check If Given Number is Palindrome or Not using While loop

  • Write a program to Check If Given Number is Palindrome or Not using While loop in C
  • Write a program to Check If Given Number is Palindrome or Not using While loop in C++
  • Write a program to Check If Given Number is Palindrome or Not using While loop in Python
  • Write a program to Check If Given Number is Palindrome or Not using While loop in PHP
  • Write a program to Check If Given Number is Palindrome or Not using While loop in Java
  • Write a program to Check If Given Number is Palindrome or Not using While loop in Java Script
  • Write a program to Check If Given Number is Palindrome or Not using While loop in C#

Explanation:

We must compare a given number with its reverse in order to determine whether it is a palindrome using a for loop. If a number reads the same both forward and backward, it is called a palindrome.

Steps

  1. Input: Accept the number.
  2. Reverse the number:
    • Extract digits of the number from the last to the first.
    • Build the reversed number by repeatedly multiplying the current reversed number by 10 and adding the last digit of the original number.
    • Use a While loop to process the digits until the number becomes 0.
  3. Compare: Check if the reversed number is equal to the original number.
  4. Output: Indicate whether the number is a palindrome.

Logic

  • Use a loop to reverse the number by extracting its last digit using the modulus operator (%).
  • Build the reversed number and compare it to the original number.

Explanation

  1. For input 121:
    • Reverse = 121.
    • Original == Reverse, so it is a palindrome.
  2. For input 123:
    • Reverse = 321.
    • Original != Reverse, so it is not a palindrome.

Program to Check If Given Number is Palindrome or Not using While loop

#include <stdio.h>

int main() {
    int num, reversedNum = 0, remainder, originalNum;

    printf("Enter an integer: ");
    scanf("%d", &num);

    originalNum = num;

    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }

    if (originalNum == reversedNum)
        printf("%d is a palindrome.\n", originalNum);
    else
        printf("%d is not a palindrome.\n", originalNum);

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int num, reversedNum = 0, remainder, originalNum;

    cout << "Enter an integer: ";
    cin >> num;

    originalNum = num;

    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }

    if (originalNum == reversedNum)
        cout << originalNum << " is a palindrome." << endl;
    else
        cout << originalNum << " is not a palindrome." << endl;

    return 0;
}

num = int(input("Enter an integer: "))
original_num = num
reversed_num = 0

while num != 0:
    remainder = num % 10
    reversed_num = reversed_num * 10 + remainder
    num //= 10

if original_num == reversed_num:
    print(f"{original_num} is a palindrome.")
else:
    print(f"{original_num} is not a palindrome.")

<?php
$num = (int)readline("Enter an integer: ");
$originalNum = $num;
$reversedNum = 0;

while ($num != 0) {
    $remainder = $num % 10;
    $reversedNum = $reversedNum * 10 + $remainder;
    $num = (int)($num / 10);
}

if ($originalNum == $reversedNum) {
    echo "$originalNum is a palindrome.\n";
} else {
    echo "$originalNum is not a palindrome.\n";
}
?>

import java.util.Scanner;

public class Palindrome {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter an integer: ");
        int num = sc.nextInt();
        int originalNum = num;
        int reversedNum = 0;

        while (num != 0) {
            int remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            num /= 10;
        }

        if (originalNum == reversedNum)
            System.out.println(originalNum + " is a palindrome.");
        else
            System.out.println(originalNum + " is not a palindrome.");

        sc.close();
    }
}

let num = parseInt(prompt("Enter an integer: "));
let originalNum = num;
let reversedNum = 0;

while (num !== 0) {
    let remainder = num % 10;
    reversedNum = reversedNum * 10 + remainder;
    num = Math.floor(num / 10);
}

if (originalNum === reversedNum) {
    console.log(`${originalNum} is a palindrome.`);
} else {
    console.log(`${originalNum} is not a palindrome.`);
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter an integer: ");
        int num = int.Parse(Console.ReadLine());
        int originalNum = num;
        int reversedNum = 0;

        while (num != 0) {
            int remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            num /= 10;
        }

        if (originalNum == reversedNum)
            Console.WriteLine(originalNum + " is a palindrome.");
        else
            Console.WriteLine(originalNum + " is not a palindrome.");
    }
}

List of All Programs