Program to Print All Palindrome Numbers Between 1 and n using While loop

Program to Print All Palindrome Numbers Between 1 and n using While loop

  • Write a program to Print All Palindrome Numbers Between 1 and n using While loop in C
  • Write a program to Print All Palindrome Numbers Between 1 and n using While loop in C++
  • Write a program to Print All Palindrome Numbers Between 1 and n using While loop in Python
  • Write a program to Print All Palindrome Numbers Between 1 and n using While loop in PHP
  • Write a program to Print All Palindrome Numbers Between 1 and n using While loop in Java
  • Write a program to Print All Palindrome Numbers Between 1 and n using While loop in Java Script
  • Write a program to Print All Palindrome Numbers Between 1 and n using While loop in C#

Explanation:

Steps

  1. Input: Accept the upper limit n.
  2. Define a palindrome check function:
    • Reverse the number and compare it to the original.
  3. Iterate from 1 to n:
    • For each number, check if it is a palindrome using the function.
    • If true, print the number.

Program to Print All Palindrome Numbers Between 1 and n using While loop

#include <stdio.h>

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

int main() {
    int n;

    printf("Enter the number up to which palindrome numbers are to be printed: ");
    scanf("%d", &n);

    int num = 1;
    while (num <= n) {
        if (isPalindrome(num)) {
            printf("%d ", num);
        }
        num++;
    }
    printf("\n");

    return 0;
}

#include <iostream>
using namespace std;

bool isPalindrome(int num) {
    int originalNum = num, reversedNum = 0, remainder;
    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }
    return (originalNum == reversedNum);
}

int main() {
    int n;

    cout << "Enter the number up to which palindrome numbers are to be printed: ";
    cin >> n;

    int num = 1;
    while (num <= n) {
        if (isPalindrome(num)) {
            cout << num << " ";
        }
        num++;
    }
    cout << endl;

    return 0;
}

def is_palindrome(num):
    original_num = num
    reversed_num = 0
    while num != 0:
        remainder = num % 10
        reversed_num = reversed_num * 10 + remainder
        num //= 10
    return original_num == reversed_num

n = int(input("Enter the number up to which palindrome numbers are to be printed: "))
num = 1

while num <= n:
    if is_palindrome(num):
        print(num, end=" ")
    num += 1
print()

<?php
function isPalindrome($num) {
    $originalNum = $num;
    $reversedNum = 0;
    
    while ($num != 0) {
        $remainder = $num % 10;
        $reversedNum = $reversedNum * 10 + $remainder;
        $num = (int)($num / 10);
    }
    return $originalNum == $reversedNum;
}

$n = (int)readline("Enter the number up to which palindrome numbers are to be printed: ");
$num = 1;

while ($num <= $n) {
    if (isPalindrome($num)) {
        echo $num . " ";
    }
    $num++;
}
echo "\n";
?>

import java.util.Scanner;

public class Palindrome {
    public static boolean isPalindrome(int num) {
        int originalNum = num, reversedNum = 0, remainder;
        while (num != 0) {
            remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            num /= 10;
        }
        return originalNum == reversedNum;
    }

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

        System.out.print("Enter the number up to which palindrome numbers are to be printed: ");
        int n = sc.nextInt();
        int num = 1;

        while (num <= n) {
            if (isPalindrome(num)) {
                System.out.print(num + " ");
            }
            num++;
        }
        System.out.println();
        sc.close();
    }
}

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

let n = parseInt(prompt("Enter the number up to which palindrome numbers are to be printed: "));
let num = 1;

while (num <= n) {
    if (isPalindrome(num)) {
        console.log(num);
    }
    num++;
}

using System;

class Program {
    static bool IsPalindrome(int num) {
        int originalNum = num, reversedNum = 0, remainder;
        while (num != 0) {
            remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            num /= 10;
        }
        return originalNum == reversedNum;
    }

    static void Main() {
        Console.Write("Enter the number up to which palindrome numbers are to be printed: ");
        int n = int.Parse(Console.ReadLine());
        int num = 1;

        while (num <= n) {
            if (IsPalindrome(num)) {
                Console.Write(num + " ");
            }
            num++;
        }
        Console.WriteLine();
    }
}

List of All Programs