Write a program to accept two numbers using Scanner class. If they are equal, display the message
“Both the numbers are equal”. If they are unequal, then find which one is greater. If the first number
is greater then, display square of the smaller number and cube of the greater number, otherwise, vice-
versa. Please answer
Answers
Question:-
Write a program to accept two numbers using Scanner class. If they are equal, display the message that "both are equal". If they are unequal then find which one is greater. If the first number is greater than the second, then display the square of the smaller number and cube of the greater number and vice versa.
Code:-
Here is the code written in Java.
import java.util.*;
class X
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
if(a==b)
System.out.println("Both are equal. ");
else if(a>b)
{
System.out.println("Cube of the greater number: "+(a*a*a);
System.out.println("Square of the smaller number: "+(b*b);
}
else
{
System.out.println("Cube of the smaller number: "+a*a*a);
System.out.println("Square of the bigger number: "+b*b);
}
}// end of main()
}// end of class.
import java.util.Scanner;
public class ReadNumberExample1.
{
public static void main(String[] args)
{
//object of the Scanner class.
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");