Page 348 - ComputerScience_Class_11
P. 348
9 for (i=0; i<4; i++)
10 {int k=i+1;
11 System.out.print("Enter "+ k + " number in the array: ");
12 ar[i] =sc.nextInt();
13 }
14 System.out.println("Enter the last number to insert: ");
15 n=sc.nextInt();
16 System.out.println("Enter the position where last number is to be added:");
17 pos=sc.nextInt();
18 if(pos>=0 && pos<=4)
19 {
20 for(i=9; i>pos; i--) // Shift the elements to the right by one position
21 {
22 ar[i]=ar[i-1];
23 }
24 ar[pos]=n;
25 }
26 for(i=0; i<10; i++)
27 {
28 System.out.print(ar[i]+ " ");
29 }
30 }
31 }
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
Enter 1 number in the array: 65
Enter 2 number in the array: 69
Enter 3 number in the array: 45
Enter 4 number in the array: 98
Enter the last number to insert:
15
Enter the position where last number is to be added:
2
65 69 15 45 98
346 Touchpad Computer Science (Ver. 3.0)-XI

