Computer Science, asked by electricbeef1, 4 months ago

write a java program on​

Attachments:

tjuan142706: ...
... ...

Answers

Answered by anindyaadhikari13
2

Required Answer:-

Question:

  • Write a program to enter two numbers. If the first number is greater than the second, display square of the smaller number and cube of the greater number otherwise vice versa.

Solution:

Here is the code.

  1. import java.util.*;
  2. public class JavaBrainly {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int a, b;
  6. System.out.print("Enter first number: ");
  7. a=sc.nextInt();
  8. System.out.print("Enter second number: ");
  9. b=sc.nextInt();
  10. if(a>b)
  11. {
  12. System.out.println("The first number is greater than the second number.");
  13. System.out.println("Square of the smaller number is: "+b*b);
  14. System.out.println("Cube of the greater number is: "+a*a*a);
  15. }
  16. else if(b>a)
  17. {
  18. System.out.println("The second number is greater than the first number.");
  19. System.out.println("Square of the greater number: "+b*b);
  20. System.out.println("Cube of the smaller number: "+a*a*a);
  21. }
  22. else
  23. System.out.println("Both are equal.");
  24. sc.close();
  25. }
  26. }

Explanation:

  • Using if else construct, this question is solved. We will check if the first number is greater than the second or not. If it's true, then we will display the square of the smaller number and cube of the greater number. If it's not true, we will display the square of the greater number and cube of the smaller number. If both are equal, a message is displayed that both the numbers are equal.

Output is attached.

Attachments:

Anonymous: super se bhi uppar ☺✨
Similar questions