Advertisements
Advertisements
Question
Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now, display only those numbers that are perfect squares.
n[0] | n[1] | n[2] | n[3] | n[4] | n[5] | n[6] | n[7] | n[8] | n[9] |
12 | 45 | 49 | 78 | 64 | 77 | 81 | 99 | 45 | 33 |
Sample Output: 49, 64, 81
Answer in Brief
Solution
import java.util.*;
public class AQ9
{ public static void main(String args[])
{Scanner ob = new Scanner(System.in);
int i, y, a[] = new int[20];
System.out.println("Enter 20 numbers");
for (i = 0; i < a.length; i++)
a[i] = ob.nextInt();
System.out.println("\nThe numbers that are Perfect Squares are");
for (i = 0; i < a.length; i++)
{y = (int) Math.sqrt(a[i]);
if (a[i] == y * y)
System.out.println(a[i]);
}}}
shaalaa.com
Is there an error in this question or solution?
Chapter 3: Arrays (Single Dimensional and Double Dimensional) - EXERCISES [Page 241]