Palindrome Number: flowchart, algorithm and program

This program reads an integer n and checks whether it reads the same forwards and backwards. It reverses the digits of n into a new number and compares that reversed number with the original value.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set reversed = 0.
  3. Step 3: Read n.
  4. Step 4: Set original = n.
  5. Step 5: If n > 0, go to step 6; otherwise go to step 9.
  6. Step 6: Set digit = n % 10.
  7. Step 7: Set reversed = reversed * 10 + digit.
  8. Step 8: Set n = n / 10. Go to step 5.
  9. Step 9: If original == reversed, go to step 10; otherwise go to step 11.
  10. Step 10: Print original is a palindrome. Go to step 12.
  11. Step 11: Print original is not a palindrome.
  12. Step 12: Stop.

Pseudocode

BEGIN
    reversed ← 0
    READ n
    original ← n
    WHILE n > 0 DO
        digit ← n % 10
        reversed ← reversed * 10 + digit
        n ← n / 10
    END WHILE
    IF original == reversed THEN
        PRINT original is a palindrome
    ELSE
        PRINT original is not a palindrome
    END IF
END

Program in C, Python and Java

Palindrome Number program in C

#include <stdio.h>

int main() {
    int n, original, reversed = 0, digit;
    printf("Enter n: ");
    scanf("%d", &n);
    original = n;
    while (n > 0) {
        digit = n % 10;
        reversed = reversed * 10 + digit;
        n = n / 10;
    }
    if (original == reversed)
        printf("%d is a palindrome\n", original);
    else
        printf("%d is not a palindrome\n", original);
    return 0;
}

Palindrome Number program in Python

n = int(input("Enter n: "))
original = n
reversed_num = 0
while n > 0:
    digit = n % 10
    reversed_num = reversed_num * 10 + digit
    n = n // 10
if original == reversed_num:
    print(original, "is a palindrome")
else:
    print(original, "is not a palindrome")

Palindrome Number program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        int original = n;
        int reversed = 0;
        int digit;
        while (n > 0) {
            digit = n % 10;
            reversed = reversed * 10 + digit;
            n = n / 10;
        }
        if (original == reversed)
            System.out.println(original + " is a palindrome");
        else
            System.out.println(original + " is not a palindrome");
    }
}

Sample input: n = 121
Output: 121 is a palindrome

How it works

  • Save the input in original before the loop, because the loop keeps cutting digits off n until it becomes 0.
  • Inside the while (n > 0) loop, digit = n % 10 picks the last digit of n.
  • reversed = reversed * 10 + digit shifts the digits already built one place left and adds the new digit at the end.
  • n = n / 10 drops the last digit, so the loop moves on to the next one.
  • After the loop, if original == reversed the number is a palindrome. For 121: reversed builds 1, then 12, then 121, which matches original.

Dry run (trace table)

StepLineBoxreversednoriginaldigitOutput
13Start
24reversed = 00
36Read n0121Enter n:
47original = n0121121
58n > 0?0121121
69digit = n % 1001211211
710reversed = reversed * 10 + digit11211211
811n = n / 101121211
98n > 0?1121211
109digit = n % 101121212
1110reversed = reversed * 10 + digit12121212
1211n = n / 101211212
138n > 0?1211212
149digit = n % 101211211
1510reversed = reversed * 10 + digit12111211
1611n = n / 1012101211
178n > 0?12101211
1813original == reversed?12101211
1914Print original is a palindrome12101211121 is a palindrome
2017Stop12101211

Time and space complexity

Time: O(log n) · Space: O(1). The loop removes one decimal digit per iteration, so it runs about log10(n) times with constant work each time.

Common mistakes

  • Forgetting to copy n into original first. By the end n is 0, so the comparison would be against the wrong value.
  • Not initialising reversed to 0, which gives garbage results because it starts with an unknown value.
  • Comparing with n instead of original after the loop, or assuming negative numbers work. With n > 0 in the loop, a negative input leaves reversed as 0 and is reported as not a palindrome.

Viva questions

Q1. Why do you store n in the variable original before the loop?

The loop keeps dividing n by 10 until it becomes 0, so the original value is lost. I save it in original so I can compare it with the reversed number at the end.

Q2. Why is reversed initialised to 0?

reversed is built up digit by digit as reversed * 10 + digit. Starting at 0 means the first digit is added without any garbage value, and 0 is the neutral starting point for this sum.

Q3. What do n % 10 and n / 10 do in this program?

n % 10 gives the last digit of n, and n / 10 is integer division that drops the last digit. Together they peel digits off from the right, one per loop pass.

Q4. Dry run the program for n = 121. What are the values of digit, reversed and n after each iteration?

Pass 1: digit=1, reversed=1, n=12. Pass 2: digit=2, reversed=12, n=1. Pass 3: digit=1, reversed=121, n=0. The loop stops, and 121 equals 121, so it prints palindrome.

Q5. What does the program print for n = 0, and for a negative number like -121?

For 0 the loop does not run (0 > 0 is false), reversed stays 0, so it says 0 is a palindrome, which is correct. For -121 the loop also never runs, reversed is 0, and 0 != -121, so it says not a palindrome, which is acceptable since negatives are usually not treated as palindromes.

Q6. Which loop is used here and why is a while loop suitable?

A while loop, because we don't know in advance how many digits the number has. We just repeat as long as n > 0, and it is an entry-controlled loop so it handles 0 safely.

Q7. What is the time complexity of this program?

O(log n), more precisely O(d) where d is the number of digits, because each iteration divides n by 10 and removes one digit. Extra space used is O(1).

Q8. What could go wrong with a large input like 1999999999, and how would you fix it?

Its reverse, 9999999991, is bigger than the int limit (about 2.1 billion), so reversed overflows and gives a wrong result. Using long long for reversed, or comparing only half of the digits, fixes it.

Q9. What would you change to make this work for negative numbers or to check a palindrome in another base?

For negatives you can decide up front to print not a palindrome, or work with the absolute value if the sign is to be ignored. For another base b, replace 10 with b in both n % b and n / b and in reversed * b.

Frequently asked questions

How does the program check if a number is a palindrome?

It reverses the digits using n % 10 and n / 10 in a loop, building the result in reversed. If reversed equals the original number, it is a palindrome.

Why do we store the number in original before the loop?

The loop divides n by 10 until it reaches 0, so the original value would be lost. original keeps it for the final comparison and for printing.

What happens for a negative number or a number ending in 0?

Negative numbers skip the loop since n > 0 is false, so reversed stays 0 and the program says not a palindrome. A number like 10 reverses to 1, which is not equal to 10, so it is correctly not a palindrome.

More programs

← All programs

Try AI Flowchart free →