Results 1 to 5 of 5

Thread: Little Help in Java

  1. #1
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Little Help in Java

    Little help i don't understand where the error is..

    The Task is


    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
    Find the largest palindrome made from the product of two 3-digit numbers.


    class Prpal
    {
    public static void main(String[] args)
    {
    int i,j;
    long a,k;
    long rev=0;
    //Repeating the Loop itself from 999 to 100
    for ( i=100; i>1000 ; i-- )
    {
    // Repeating the loop to multiply i with numbers from 999 to 100 Each time i value is increased
    for (j =100; j<i; j++)
    {
    k=i*j;
    // Logic for Reversing The Number
    a=k%10;
    k=k/10;
    rev=rev*10+a;

    // Checking weather the given number is palindrome or not??
    if (k==rev)
    {
    System.out.println("The Number "+k+"is Palindrome");
    }

    }
    }
    }
    }

    It is compiling without Errors yet no output is displaying..
    I can't understand where the error is....

  2. #2
    Join Date
    Nov 2008
    Location
    Lleida, Spain
    Beans
    1,157
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Little Help in Java

    The first for loop condition never works:

    for (i=100; i>1000; i--)

    if i=100 and the condition to enter the loop is i > 1000, this never will enter the loop.

  3. #3
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Little Help in Java

    Quote Originally Posted by albandy View Post
    The first for loop condition never works:

    for (i=100; i>1000; i--)

    if i=100 and the condition to enter the loop is i > 1000, this never will enter the loop.
    Nope no result same as Blank........

    i changed for loop like

    for(i=100;i<1000;i++)
    {
    for(j=100;j<i;j++)
    {
    k=i*j;
    a=k%10;
    k=k/10;
    rev=rev*10+a;
    if (k==rev)
    {
    System.out.println("The Number "+k+"is Palindrome");
    }
    }
    }

    yet no avail
    Last edited by Mohan1289; October 6th, 2012 at 09:24 AM.

  4. #4
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Little Help in Java

    That implementation of the test for a palindromic number is not what was discussed before. It is simply wrong. Go back to the previous thread and implement that - if you are still minded not to use a string. Hint - you need another loop.

    Here's an example of what your code is doing:
    Code:
    k = 423324
    a=4
    k=42332
    rev=0*10+4
    if (42332 == 4)
    Put your algorithm for testing the palindromic number into a separate function that you can test. Test it.

    Also, your inner loop means that you will never multiply a number by itself. I don't know whether that is your intent or not. Perhaps there are no squares that are palindromic in the given range - I don't know.

  5. #5
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Little Help in Java

    Quote Originally Posted by spjackson View Post
    That implementation of the test for a palindromic number is not what was discussed before. It is simply wrong. Go back to the previous thread and implement that - if you are still minded not to use a string. Hint - you need another loop.

    Here's an example of what your code is doing:
    Code:
    k = 423324
    a=4
    k=42332
    rev=0*10+4
    if (42332 == 4)
    Put your algorithm for testing the palindromic number into a separate function that you can test. Test it.

    Also, your inner loop means that you will never multiply a number by itself. I don't know whether that is your intent or not. Perhaps there are no squares that are palindromic in the given range - I don't know.
    yes yes i see my flaw now.. i have to stop the loop when it reaches to Zero... I mean when all the numbers are over.. now it is just doing only once right???

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •