-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlastzero.java
More file actions
42 lines (37 loc) · 997 Bytes
/
lastzero.java
File metadata and controls
42 lines (37 loc) · 997 Bytes
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
42
import java.util.Scanner;
/**
* lastzero
*/
public class lastzero {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an array: ");
int n = sc.nextInt();
System.out.println("Enter the array elements:");
int []arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
System.out.println("Original array");
for(int ele : arr){
System.out.println(ele+" ");
}
movezero(arr,n);
System.out.println("Modified array ");
for(int ele : arr){
System.out.println(ele +" ");
}
sc.close();
}
public static void movezero(int arr[], int n){
int nonzero = 0;
for(int i=0;i<n;i++){
if(arr[i]!=0){
arr[nonzero++] = arr[i];
}
}
while(nonzero < n){
arr[nonzero++] = 0;
}
}
}