Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

computer science 1000 In mathematics, a perfect square is an integer that is the

ID: 675083 • Letter: C

Question

computer science 1000

In mathematics, a perfect square is an integer that is the square of an integer. In other words, a perfect square is the product of an integer with itself. (Source: Wiki') For example: 1 is a perfect square since it can be written as 1 times 1 ... 64 is a perfect square since it can be written as 8times8 81 is a perfect square since it can be written as 9times9 ... Write a program that can determine whether a number is a perfect square. The main() method will just include a simple loop which asks users to input an integer number and call an identification method to determine whether the input integer number is a perfect square or not. After each identification process the program will keep asking user to make input until user input "-1". To process each user input, write a method that will take an integer number as parameter and returns true/false if the integer is/isn't a perfect square. You can use the following method declaration: public static boolean is Perfect Square (int input) Sample Output Perfect Square Identifier (enter -1 to quite) Please enter a number: 10 [Enter] 10 is not a Perfect Square. Please enter a number: 9 [Enter] 9 is a Perfect Square. Please enter a number: 64 [Enter] 64 is a Perfect Square. Please enter a number: -1 [Enter] Goodbye. :) End of processing...

Explanation / Answer

import java.io.*;
import java.util.Scanner;
import java.lang.Math;

class IsPerfectSquare
{
public static boolean isPerfectSquare(int input)
{
if(Math.pow(Math.sqrt(input), 2) == input)
return true;
return false;
}
public static void main(String[] args)
{
int input = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Perfect Square Identifier (Enter -1 to quit.)");
while(input != -1)
{
System.out.print("Please enter a number: ");
input = sc.nextInt();
if(input == -1)
break;
if(isPerfectSquare(input))
System.out.println(input+" is a Perfect Square.");
else
System.out.println(input+" is not a Perfect Square.");
}
System.out.println("Goodbye. :) End of processing...");

}
}