"An array is used to store ten integer numbers. Write a Java program that determines and print the square numbers which are also odd numbers from the given array."
The problem I have is how to figure out if the number in the array is a square number. I tried this way but it's not correct!
import java.math.*;
public class JavaApplication43 {
public static void main(String[] args) {
int[] no = {22, 44, 25, 89, 81, 55, 23, 25, 55};
for (int i = 0; i < no.length; i++) {
int x = no[i];
double y;
if (x % 2 != 0) {
y = Math.sqrt(x);
if (x == (Math.pow(y, 2)))
System.out.println(no[i]);
}
}
}
}
This is the output it gives me
run:
25
81
55
25
55
55
is there too that means this method I used is not successful!
You could just do:
for (int i = 0; i < no.length; i++) {
int x = no[i];
if (x % 2 == 0) continue;
int y = (int) Math.sqrt(x);
if (x == y * y) {
System.out.println(x);
}
}