Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:31:01+00:00 2026-06-14T21:31:01+00:00

This code gets an integer n and displays all palindrome numbers less than n.

  • 0

This code gets an integer n and displays all palindrome numbers less than n.
But seems the for loop doesn’t work; because when I enter a number except 0, 1 and negatives, nothing happens.
I tried debugging, but couldn’t find the problem!

Sample input: 30

Sample output: 1 2 3 4 5 6 7 8 9 11 22

import java.util.Scanner;

public class PalindromeNumbers {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        if (n == 0 || n == 1 || n < 0)
            System.out.println("There's no Palindrome Number!");
        else {
            for (int num = 1; num < n; num++) {
                int reversedNum = 0;
                int temp = 0;
                while (num > 0) {

                    // use modulus operator to strip off the last digit
                    temp = num % 10;

                    // create the reversed number
                    reversedNum = reversedNum * 10 + temp;
                    num = num / 10;

                }
                if (reversedNum == num)
                    System.out.println(num);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:31:02+00:00Added an answer on June 14, 2026 at 9:31 pm

    You run into an infinite loop: you use num in your for loop as an index, and reset it to 0 inside the loop. Use different variables, and it should work!

    for (int i = 1; i < n; i++) {
        int num = i;
    
        ...
    
        if (reversedNum == i)
            System.out.println(i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which gets WP posts, but it gets all the posts
This code gets html data and should replace all javascript content $.ajax({ url: url,
This is the first time I got this error. This code basically gets the
I have this code. Only the first drawLine gets drawn and the rest 2
I have this code that reads from XML file. It gets five strings (groupId,
I am working on a code which requires a function. This function gets a
This code below allows me to find the word error in all my files
I have to write this code for a homework assignment but I don't even
I want to know why this piece of code gets a compilation error. I'm
When i am Building this code i am getting WARNING that INVALID Integer to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.