KnowledgeBoat Logo
|

Computer Applications

Write a program to accept a number and check whether the number is a perfect square or not.
Sample Input: 49
Sample Output: A perfect square

Java

Input in Java

24 Likes

Answer

import java.io.*;

public class KboatPerfectSquare
{
    public static void main(String args[]) throws IOException {
        InputStreamReader read = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(read);
        
        System.out.print("Enter the number: ");
        int a = Integer.parseInt(in.readLine());
        
        double srt = Math.sqrt(a);
        double diff = srt - Math.floor(srt);
        
        if (diff == 0) {
            System.out.println(a + " is a perfect square");
        }
        else {
            System.out.println(a + " is not a perfect square");
        }
    }
}

Output

BlueJ output of Write a program to accept a number and check whether the number is a perfect square or not. Sample Input: 49 Sample Output: A perfect squareBlueJ output of Write a program to accept a number and check whether the number is a perfect square or not. Sample Input: 49 Sample Output: A perfect square

Answered By

9 Likes


Related Questions