# Perfect Squares

• 02-04-2008, 03:48 PM
divyachaparala
Perfect Squares
Hi All,

Am new to java and i got a assignment on perfect squares can please help me how to do that... but not by using Math.Sqrt all these methods.. Please help me atleast give some ideas....

Thanks
• 02-04-2008, 08:28 PM
tim
Welcome to the forums!
Hello divyachaparala.

Your question is a bit vague. Are you looking for all the perfect squares up to some integer? Please ask your question clearly and explain what is expected for your assignment. ;)
• 02-05-2008, 08:45 AM
Cnu
import java.util.*;
class PerfectSquares
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int i=1;
while(i<=n)
{
System.out.println("Square of"+i+"is"+i*i);
i++;
}//end of while
}//end of main()
}//end of class
• 02-05-2008, 10:03 AM
divyachaparala
Quote:

Originally Posted by Cnu
import java.util.*;
class PerfectSquares
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int i=1;
while(i<=n)
{
System.out.println("Square of"+i+"is"+i*i);
i++;
}//end of while
}//end of main()
}//end of class

hi
sorry this is not i want...

if we give a number it should tell us weather its a perfect square are not....
Thanks.
• 02-05-2008, 10:21 AM
praveena
public class PerfectSquare{

public static void main(String args[]){

InputStream in= new InputStream(System.in);
int i,no;
boolean flag=false;
no=Integer.parseInt(str);
for(i=0;i<no/2;i++)
{
if(i*i==no)
{
System.out.println("The no is a perfect square..");
flag=true;
break;

}//if
}//for
if(!flag)
System.out.println("The no is not perfect square..");
}//main
}//class

You can also use the basic principle of perfect squares..

Only perfect squares have odd no of factors..

Eg: factors(25)= 1,5,25