Computer Applications
Write a program to input two unequal positive numbers and check whether they are perfect square numbers or not. If the user enters a negative number then the program displays the message 'Square root of a negative number can't be determined'.
Sample Input: 81, 100
Sample Output: They are perfect square numbers.
Sample Input: 225, 99
Sample Output: 225 is a perfect square number.
99 is not a perfect square number.
Answer
import java.util.Scanner;
public class KboatPerfectSquare
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
if (a < 0 || b < 0) {
System.out.println("Square root of a negative number can't be determined");
}
else {
double sqrtA = Math.sqrt(a);
double sqrtB = Math.sqrt(b);
double isAPerfectSq = sqrtA - Math.floor(sqrtA);
double isBPerfectSq = sqrtB - Math.floor(sqrtB);
if (isAPerfectSq == 0 && isBPerfectSq == 0) {
System.out.println("They are perfect square numbers.");
}
else if (isAPerfectSq == 0) {
System.out.println(a + " is a perfect square number.");
System.out.println(b + " is not a perfect square number.");
}
else if (isBPerfectSq == 0) {
System.out.println(a + " is not a perfect square number.");
System.out.println(b + " is a perfect square number.");
}
else {
System.out.println("Both are not perfect square numbers.");
}
}
}
}
Variable Description Table
Program Explanation
Output
Related Questions
Write a program to input three unequal numbers. Display the greatest and the smallest number.
Sample Input: 28, 98, 56
Sample Output: Greatest Number: 98
Smallest Number: 28Write a program to input year and check whether it is:
(a) a Leap year
(b) a Century Leap year
(c) a Century year but not a Leap year
Sample Input: 2000
Sample Output: It is a Century Leap Year.Without using if-else statement and ternary operators, accept three unequal numbers and display the second smallest number.
[Hint: Use Math.max( ) and Math.min( )]
Sample Input: 34, 82, 61
Sample Output: 61Write a program to accept a number and check whether the number is divisible by 3 as well as 5. Otherwise, decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?
The program displays the message accordingly.