-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductArray.java
More file actions
48 lines (34 loc) · 925 Bytes
/
ProductArray.java
File metadata and controls
48 lines (34 loc) · 925 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
43
44
45
46
47
48
//Product of Array except self
class ProductArray{
public static void main(String args[]){
int arr[]={1,2,3,4,5};
int n=5;
int p=1;
boolean flag=false;
for(int i=0;i<n;i++){
if(arr[i]!=0){
p*=arr[i];
}
else{
flag=true;
}
}
int result[]=new int[n];
for(int i=0;i<n;i++){
if(arr[i]==0){
result[i]=p;
}
else{
if(flag){
result[i]=0;
}
else{
result[i]=p/arr[i];
}
}
}
for(int i=0;i<n;i++){
System.out.print(result[i]+" ");
}
}
}