Computer Science, asked by av502206, 6 days ago

Write a if program in Java to input a number (greater than zero) and if
(a) it is an odd positive number then print its square and cube.
(b) it is an even positive number then print sum of its square and square root. ​

Answers

Answered by Shivu516
1

Hope it helps ^_^

Please mark my answer as Brainliest if you think that it's good :)

import java.util.Scanner;

public class Conditional_Program{

   public static void main(String [] args){

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter a number greater than 0 : ");

       double num = sc.nextDouble();

       if (num > 0){

           if (num % 2 != 0){

               System.out.println("Square: " + Math.pow(num,2));

               System.out.println("Cube: " + Math.pow(num,3));

           }

       else if (num % 2 == 0){

           double sq = Math.pow(num,2);

           double sqrt = Math.sqrt(num);

           System.out.print("Sum of square and square root: " + (sq + sqrt));

           }

       }

       else  

       System.out.println("Invalid Input *_*");

   }

}

Output:

(i)

Enter a number greater than 0 : 5

Square: 25.0

Cube: 125.0

(ii)

Enter a number greater than 0 : 16

The sum of square and square root: 260.0

(iii)

Enter a number greater than 0 : -10

Invalid Input *_*

Similar questions
History, 3 days ago