Palindromes in a Range

Last Updated : 14 Jun, 2026

Given two integers m and n, find all palindrome numbers between m and n (inclusive).

Examples:

Input: m = 10, n = 115
Output: [11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111]
Explanation: The palindrome numbers in the range [10, 115] are 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, and 111.

Input: m = 2, n = 5
Output: [2, 3, 4, 5]
Explanation: All numbers in the range [2, 5] are palindrome numbers.

Try It Yourself
redirect icon

[Expected Approach] By Reversing Digits of Each Number

Iterate through all numbers in the range [m, n]. For each number, reverse its digits mathematically and compare the reversed value with the original number. If both are equal, the number is a palindrome and is added to the answer.

Step by Step Illustration:

  • Let's take m = 10 and n = 25.
  • For number 10:
    - Reverse of 10 = 01
    - Since both are different, 10 is not a palindrome.
  • For number 11:
    - Reverse of 11 = 11
    - Since both are equal, 11 is a palindrome.
  • Continuing this process for all numbers in the range, the palindrome numbers are: [11, 22]
C++
#include <bits/stdc++.h>
using namespace std;

vector<int> printPalindromes(int m, int n) {
    vector<int> ans;

    for (int num = m; num <= n; num++) {
        int rev = 0;
        int original = num;

        // Reverse the digits of the current number.
        while (original > 0) {
            rev = rev * 10 + original % 10;
            original /= 10;
        }

        // If the reversed number is same as the original,
        // then it is a palindrome.
        if (rev == num) {
            ans.push_back(num);
        }
    }

    return ans;
}

int main() {
    int m = 10;
    int n = 115;

    vector<int> ans = printPalindromes(m, n);

    for (int x : ans) {
        cout << x << " ";
    }

    return 0;
}
Java
import java.util.ArrayList;

public class GFG {

    static ArrayList<Integer> printPalindromes(int m, int n) {
        ArrayList<Integer> ans = new ArrayList<>();

        for (int num = m; num <= n; num++) {
            int rev = 0;
            int original = num;

            // Reverse the digits of the current number.
            while (original > 0) {
                rev = rev * 10 + original % 10;
                original /= 10;
            }

            // If the reversed number is same as the original,
            // then it is a palindrome.
            if (rev == num) {
                ans.add(num);
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        int m = 10;
        int n = 115;

        ArrayList<Integer> ans = printPalindromes(m, n);

        for (int x : ans) {
            System.out.print(x + " ");
        }
    }
}
Python
def printPalindromes(m, n):
    ans = []

    for num in range(m, n + 1):
        rev = 0
        original = num

        # Reverse the digits of the current number.
        while original > 0:
            rev = rev * 10 + original % 10
            original //= 10

        # If the reversed number is same as the original,
        # then it is a palindrome.
        if rev == num:
            ans.append(num)

    return ans


m = 10
n = 115

print(*printPalindromes(m, n))
C#
using System;
using System.Collections.Generic;

class GFG
{
    static List<int> PrintPalindromes(int m, int n)
    {
        List<int> ans = new List<int>();

        for (int num = m; num <= n; num++)
        {
            int rev = 0;
            int original = num;

            // Reverse the digits of the current number.
            while (original > 0)
            {
                rev = rev * 10 + original % 10;
                original /= 10;
            }

            // If the reversed number is same as the original,
            // then it is a palindrome.
            if (rev == num)
            {
                ans.Add(num);
            }
        }

        return ans;
    }

    static void Main()
    {
        int m = 10;
        int n = 115;

        List<int> ans = PrintPalindromes(m, n);

        Console.WriteLine(string.Join(" ", ans));
    }
}
JavaScript
function printPalindromes(m, n) {
    const ans = [];

    for (let num = m; num <= n; num++) {
        let rev = 0;
        let original = num;

        // Reverse the digits of the current number.
        while (original > 0) {
            rev = rev * 10 + (original % 10);
            original = Math.floor(original / 10);
        }

        // If the reversed number is same as the original,
        // then it is a palindrome.
        if (rev === num) {
            ans.push(num);
        }
    }

    return ans;
}

// Driver Code
const m = 10;
const n = 115;

console.log(printPalindromes(m, n).join(" "));

Output
11 22 33 44 55 66 77 88 99 101 111 

Time Complexity: O((n - m) * d) - Each number in the range is checked once, and reversing a number takes O(d) time, where d is the number of digits.
Auxiliary Space: O(1) - Only a constant amount of extra space is used.

Comment