简体   繁体   中英

Can someone please explain why this code wont work?

I have been given a program in my class about taking an integer user input from the user between 2 to 10 (n) and print all 'n' digited palindrome numbers.I have come up with a code,but it seems it is not working properly.Myself,I cannot find a logical error,so can someone please help with this code and suggest any changes?

A palindrome number is one which is the same when read from either side of the number.

    import java.util.Scanner;
    public class npalindrome
    {
    public static void main()
    {
    int i,j,k,l=0,n,r=1,p;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your number between 2 to 10");
    n=sc.nextInt();
    for(i=(int)Math.pow(10,n-1);i<=((int)Math.pow(10,n)-1);i++)
    {
        k=i;
        r=0;

        l=0;

        while(k>=0)
        {
            r=k%10;

            l=(l*10)+r;

            k=k/10;
        }
        if(l==i)
        {
            System.out.println(i+" ");
        }
    }
}

There is no output so to speak of, the program runs in an endless loop

Because your k is always greater or equal to 0. So you are stuck in your while:

while(k>=0)

Maybe you want to change that to just while(k>0) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM