Results 1 to 5 of 5

Thread: Perfect Squares

  1. #1
    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    Unhappy 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

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default 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. ;)
    Last edited by tim; 02-04-2008 at 08:30 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    Cnu
    Cnu is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default

    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

  4. #4
    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Cnu View Post
    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.

  5. #5
    praveena is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    public class PerfectSquare{

    public static void main(String args[]){

    InputStream in= new InputStream(System.in);
    int i,no;
    boolean flag=false;
    String str=in.readLine();
    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

Similar Threads

  1. Way to Java Perfect
    By Javaisinmyblood in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 12:28 AM
  2. Help with code "perfect shuffle"
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •