Advertisement
Hiteshw11

Replace Number At A Specified Position In Array

Jan 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.*;
  2. class SpecifiedPosition {
  3.     public static void main(String[] args) {
  4.         int n;
  5.         Scanner src=new Scanner(System.in);
  6.         System.out.println("Enter the number of elements");
  7.         n=src.nextInt();
  8.         int a[]=new int[n];
  9.         for(int i=0;i<n;i++)
  10.         {
  11.             System.out.println("Enter the element");
  12.             a[i]=src.nextInt();
  13.         }
  14.         System.out.println("The elements are");
  15.         for(int j=0;j<n;j++)
  16.         {
  17.             System.out.println(a[j]);
  18.         }
  19.         System.out.println("Enter the position at which you want to put element");
  20.         int p=src.nextInt();
  21.         System.out.println("Enter the element");
  22.         int ele=src.nextInt();
  23.         a[p-1]=ele;
  24.         System.out.println("Elements in the array after inserting are");
  25.         for(int k=0;k<n;k++)
  26.         {
  27.             System.out.println(a[k]);
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement