-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion.java
More file actions
41 lines (33 loc) · 1.37 KB
/
Copy pathinsertion.java
File metadata and controls
41 lines (33 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Arrays;
import java.util.Scanner;
public class insertion{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of array : ");
int [] arr = new int[sc.nextInt()];
System.out.println("Enter the elements of the array ");
for(int i=0; i<arr.length;i++){
arr[i] = sc.nextInt();
}
System.out.print("Enter the number you want to be change in array : ");
int chnageNumber = sc.nextInt();
System.out.print("Now enter the new number you want to replace with : ");
int changeewith = sc.nextInt();
replaceElement(arr, chnageNumber, changeewith);
}
public static void replaceElement(int array[] , int replaceNumber , int replaceWith){
boolean found = false;
for(int i =0; i <array.length; i++){
if(array[i]==replaceNumber){
array[i] = replaceWith;
found = true;
System.out.println("The number " + replaceNumber + " is replaced at " + i + " with "+ array[i] );
}
}
String str = Arrays.toString(array);
System.out.println(str);
if(!found){
System.out.println("The number " + replaceNumber + " is not found in the array ");
}
}
}