KnowledgeBoat Logo

Computer Applications

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[16]n[17]n[18]n[19]
12454978647781994533

Sample Output: 49, 64, 81

Java

Java Arrays

79 Likes

Answer

import java.util.Scanner;

public class KboatSDASquares
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int arr[] = new int[20];
        
        System.out.println("Enter 20 numbers");
        for (int i = 0; i < arr.length; i++) {
            arr[i] = in.nextInt();
        }
        
        System.out.println("Perfect Squares are:");
        for (int i = 0; i < arr.length; i++) {
            double sr = Math.sqrt(arr[i]);
            if ((sr - Math.floor(sr)) == 0)
                System.out.print(arr[i] + ", ");
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now, display only those numbers that are perfect squares. Sample Output: 49, 64, 81

Answered By

31 Likes


Related Questions