Friday, August 26, 2011

Perfect Square example in java


//Find out your given number is perfect square number or not.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PerfectSquareExample
{
public static void main(String args[]) throws IOException
{

int a;
float b,f1,f2;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter a numbers:");
f1=Float.valueOf(br.readLine()).floatValue();

b=(float)Math.sqrt(f1);
a=(int)Math.sqrt(f1);
f2=(float)a;

if(b==f2)
System.out.println(f1+" is a perfect square number.");
else
System.out.println(f1+" is not a perfect square number");
}
}

Output1 =>
Enter a numbers:
64
64.0 is a perfect square number.

Output2 =>
Enter a numbers:
35
35.0 is not a perfect square number

No comments:

Post a Comment