Exercise in Array Basics USING JAVA 1 Declare an int array o

Exercise in Array Basics USING JAVA

1. Declare an int array of size 5

2. Initialize the array to contain the numbers 10, 20, 30, 40, 50 (may be combined with step 1 above) – using a loop

     - Draw a flowchart

     - Write the relevant algorithm

     - Translate to code

Solution

ArrayOf5Nos.java

import java.util.Scanner;

public class ArrayOf5Nos {

   public static void main(String[] args) {
       //Creating an array of size 5 which is of type integer
       int arr[]=new int[5];
      
       //Scanner object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
  
/* This loop will get the values from the user
* and populate them into an array
*/
for(int i=0;i<5;i++)
{
   System.out.print(\"Enter number \"+(i+1)+\":\");
   arr[i]=sc.nextInt();
}
  
//Displaying the elements in the array
System.out.println(\"The elements in the array are :\");
for(int i=0;i<5;i++)
{
   System.out.print(arr[i]+\" \");
}

   }

}

_______________________

Output:

Enter number 1:11
Enter number 2:22
Enter number 3:33
Enter number 4:44
Enter number 5:55
The elements in the array are :
11 22 33 44 55

___________________

Exercise in Array Basics USING JAVA 1. Declare an int array of size 5 2. Initialize the array to contain the numbers 10, 20, 30, 40, 50 (may be combined with st

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site