Pr
2. write a program in which the program asks the user for a number. The program
then prints a lime, indicating whether the number is greater than zero or not; if it
isgreater than zero it finds square else the program stops at the line "the number is
less thanorequal to zero".
Answers
Answered by
2
Answer:
The required Java program is :-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System . in);
System.out.print("Enter a number (Integer of decimal number) : ");
double num = scan.nextDouble();
scan.close();
if(num > 0) {
double squareNum = Math.pow(num,2);
System.out.println("The number is greater than zero and its square is "+squareNum);
}
else {
System.out.println("The number is less than or equal to zero.");
}
}
}
Note :-
The above códe is saved in a file named Main.java .
Also, condition in if statement can be changed and accordingly, the output statements will also change.
Similar questions