0

This is my first time using this and I'm having a bit of trouble with arrays. I need to check if the values in an array that the user enters are continuously being squared such as {2, 4, 16, 256}. Checking if num[1] = num[0] * num[0], num[2] = num[1] * num[1] and so on. Any help would be appreciated.

import java.util.Scanner;

public class PS6Square {
    public static void main (String [] args){

        Scanner scan = new Scanner(System.in);
        System.out.println("How many values would you like to enter?");
        String input = scan.next();
        int array = Integer.parseInt(input);
        int num[] = new int[array];
        int i = 0;
        boolean result = false;

        for(i = 0; i < num.length; i++)
        {
            System.out.print("Enter a value:\t");
            num[i] = scan.nextInt();
            if(num[i] == num[i] * num[i])
            {
                result = true;
            }
        }

        System.out.println("\nThe result is:\t" + result);
    }
}
JWash33
  • 1
  • 1
  • Possible duplicate of [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer) – user2664856 Nov 02 '16 at 21:21

1 Answers1

0

This question has already been asked. Try the search function next time.

But here is a solution:

 double sqrt = Math.sqrt(int_to_test);
 int x = (int) sqrt;
 if(Math.pow(sqrt,2) == Math.pow(x,2)){
 //it is a perfect square
 }
user2664856
  • 630
  • 14
  • 30