Sum and Product Program in java


Question:

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”

Write a program to help Lisa find out the lucky winner. The program should accept two numbers, check the logic and display the message as shown in the sample output.

Sample input 1

24

3

Sample output 1

You are Lucky! Here Is your Gift.

Sample input 2

46

2

Sample output 2Better Luck Next Time

Sample input

30
Sample output 3
Invalid Input

Sample input

489

0
Sample output 4

Invalid Input

CODE:

import java.util.*;
public class Main 
{
    public static void main (String[] args) {
        Scanner sc= new Scanner(System.in);
        Main obj=new Main();
        int a=sc.nextInt();
        if(a>0)
        {
            int b=sc.nextInt();
            if(b>0)
            { int k=obj.rvs(a+b);
                if(k==a*b)
                {
                    System.out.println("You are Lucky! Here Is your Gift.");
                }
                
                else
                {
                    System.out.println("Better Luck Next Time");   
                }
            }
            else
            {
                System.out.println("Invalid Input");
            }
        }
        
        else
        {
            System.out.println("Invalid Input");
        }
    }
     int rvs(int num)
     {int ans=0;
         while(num>0)
         {
             ans=ans*10+num%10;
             num/=10;
         }
         return ans;
     }
}


 Tags:

java interview programs for experienced | java interview programs for freshers pdf | java coding questions for 5 years experience | java programming interview questions – geeksforgeeks |
java programming interview questions and answers for freshers | java programs asked in interview for automation tester | java coding interview questions for experienced professionals
| java coding interview questions pdf | code | 2021

Previous
Next Post »