0% found this document useful (0 votes)
5 views1 page

Array Dsa

The Java program allows users to create an array of integers, input its size and elements, and insert a new number at a specified position. It checks for valid array size and position before inserting the new element. The program then displays the updated array elements after insertion.

Uploaded by

Asfand Yar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Array Dsa

The Java program allows users to create an array of integers, input its size and elements, and insert a new number at a specified position. It checks for valid array size and position before inserting the new element. The program then displays the updated array elements after insertion.

Uploaded by

Asfand Yar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;
public class Arraytraversal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size, i, numb, pos;
int[] array = new int[10];
System.out.println("enter size of array");
size = input.nextInt();
System.out.println("Enter elements of array");
for (i = 0; i < size; i++) {
//checking if size of array if not filled or over.
if (size >= array.length || size == array.length) {
System.out.println("YOU CAN NOT INSERT ELEMENTS IN ARRAY");
}
//if size is not exceeding array length than enter elements in array
else {
array[i] = input.nextInt();
}
}
//here enter the element in exiting array you want to enter
System.out.println("enter number you want to enter in array");
numb = input.nextInt();
//here enter the position where you want to enter the element in array
System.out.println("enter position where you want to enter number in
array");
pos = input.nextInt();
//i=size-1 is index of the last element in array
// loop will start from last index and move backward
for (i = size - 1; i >= pos - 1; i--) {
if (pos <= 0 && pos >= size) {
System.out.println("Inavlid Position");
} else {
array[i + 1] = array[i];
//enter the element(numb) at the position[pos-1] here
array[pos-1] = numb;
//increasing the size because a new element has been added to the
array
size++;
}
System.out.println("THE ELEMENTS IN ARRAY ARE :");
for (i = 0; i < size; i++) {
System.out.println(array[i]);
}
}
}
}

You might also like