The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
5 elements array
By Guest on 7th October 2024 07:21:02 AM | Syntax: JAVASCRIPT | Views: 34



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. import java.util.Scanner;
  2.  
  3. public class ArrayExample {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         // Ask the user how many elements to input
  8.         System.out.print("Enter the number of elements: ");
  9.         int t = scanner.nextInt();
  10.  
  11.         // Declare an array to hold the elements
  12.         int[] array = new int[t];
  13.  
  14.         // Read t elements into the array
  15.         System.out.println("Enter " + t + " elements:");
  16.         for (int i = 0; i < t; i++) {
  17.             array[i] = scanner.nextInt();
  18.         }
  19.  
  20.         // Display the elements of the array
  21.         System.out.println("The elements in the array are:");
  22.         for (int i = 0; i < t; i++) {
  23.             System.out.print(array[i] + " ");
  24.         }
  25.     }
  26. }





elements array    





  • Recent Pastes