0

I'm a beginner to JAVA. I have this school problem: Lisa is hosting a party, during which she has planned for surprise gifts for her guests.

The guests while entering the hall should pick two slips of paper upon which numbers are written.

At the end of the party, the guests should bring their slip of papers to Lisa. The Lucky ones are those who received numbers that satisfy the following condition.

The sum of the two numbers is the reverse of the product of the two numbers.

For example, If a guest has got X and Y as the two numbers, he will be a winner only if

X+ Y= AB; Then X * Y =BA.

Note : Both X and Y should be greater than 0. Otherwise print "Invalid Input"

Sample input 1

24

3

Sample output 1

You are Lucky! Here Is your Gift.

Sample input 2

46

2

Sample output 2

Better Luck Next Time

Sample input 3

0

Sample output 3

Invalid Input

Sample input 4

89

0

Sample output 4

Invalid Input

Code I wrote:

        if(num1>0) 
        {
         System.out.print("Enter second number: ");
         int num2=sc.nextInt();

         if(num2<=0)
         System.out.print("Invalid Input");
         else
           {
            sum=num1+num2;
            product=num1*num2;

            i=product%10;
            product=product/10;
            revproduct=(i*10)+product; 

             if(sum==revproduct)
             System.out.print("You are Lucky! Here Is your Gift."); 
             else
             System.out.print("Better Luck Next Time");
            }      
       else
       System.out.print("Invalid Input");
     }

Error: One test case failed. Same sum and product check

zeus
  • 126
  • 1
  • 11
  • Did you try for larger inputs? – Himanshu Apr 20 '20 at 19:22
  • 1
    @HimanshuAhuja No, I only tried a combination of inputs that have two digit sum and product. – zeus Apr 20 '20 at 19:25
  • 1
    That's your problem. E.g. `2 + 497 = 499 <-> 994 = 2 * 497` or `11 + 110 = 0121 <-> 1210 = 11 * 110` --- Or how about single-digit? `2 + 2 = 4 <-> 4 = 2 * 2` or `9 + 9 = 18 <-> 81 = 9 * 9` – Andreas Apr 20 '20 at 19:27
  • What test case failed? (`product >= 100`?;) ...your `revproduct` looks ok but works only for 1-99 (max 2 digits)!? – xerx593 Apr 20 '20 at 19:29
  • @xerx593 `revproduct` only works for 10-99 (exactly 2 digits)! – Andreas Apr 20 '20 at 19:31
  • 1
    @Andreas how do I resolve this error? any idea – zeus Apr 20 '20 at 19:31
  • 1
    @xerx593 the compiler doesn't mention the failed test case. It just displays: One test case failed. Same sum and product check. – zeus Apr 20 '20 at 19:32
  • @zeus You do a web search for [`how to reverse a number`](https://www.google.com/search?q=how+to+reverse+a+number) – Andreas Apr 20 '20 at 19:34
  • 1
    @xerx593 yes I get it, revproduct works for 2 digits only. Should I use a loop to extend it's range or better approach? – zeus Apr 20 '20 at 19:35
  • https://stackoverflow.com/questions/3806126/java-reverse-an-int-value-without-using-array ;) (2nd answer my favourite...yes, you'd do it in a loop) – xerx593 Apr 20 '20 at 19:36
  • 1
    https://stackoverflow.com/questions/3806126/java-reverse-an-int-value-without-using-array – leo Apr 21 '20 at 07:59
  • You need to modify **revproduct** function https://stackoverflow.com/questions/3806126/java-reverse-an-int-value-without-using-array – leo Apr 21 '20 at 08:04

2 Answers2

1
while (input != 0) 
{    
    last_digit = input % 10;
    if (last_digit % 2 != 0) {     
        reversedNum = reversedNum * 10 + last_digit;

    }
    input = input / 10; 
}
leo
  • 46
  • 5
0
class Main
{
    public static int reverse(int n)
    {
        int reverse = 0;
        while (n != 0)
        {
            reverse = reverse * 10;
            reverse = reverse + n % 10;
            n = n / 10;
        }
        return reverse;
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int a, b;
        a = sc.nextInt();
        if (a <= 0)
        {
            System.out.println("Invalid Input");
        } else
        {

            b = sc.nextInt();

            if (b <= 0)
            {
                System.out.println("Invalid Input");
            } else
            {
                if (a + b == reverse(a * b))
                {
                    System.out.println("You are Lucky! Here Is your Gift");
                } else
                {
                    System.out.println("Better Luck Next Time");
                }
            }
        }
    }
}
Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54