Program to check perfect square in java

Updated: Apr 2

Simple Java program to check whether a number is perfect square or not | Java Programming

Problem:

Write a program to check whether a given number is a perfect square or not a perfect square, in java.

Code:

import java.util.Scanner;

public class checkPerfectSquare{
     public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number to check: ");
        double num = sc.nextDouble();
        sc.close();
        double d=Math.sqrt(num);
        if (d-Math.floor(d)==0){
		  System.out.print(num+ " is a perfect square number.");
        }
	    else{
		  System.out.print(num+ " is not a perfect square number.");
        }
     }
}

21 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes