fork download
  1. class GFG{
  2. static void ReplaceElements(int arr[], int n)
  3. {
  4. int prod = 1;
  5.  
  6. // Calculate the product of all the elements
  7. for (int i = 0; i < n; ++i) {
  8. prod *= arr[i];
  9. }
  10.  
  11. // Replace every element product
  12. // of all other elements
  13. for (int i = 0; i < n; ++i) {
  14. arr[i] = prod / arr[i];
  15. }
  16. }
  17.  
  18. public static void main(String[] args)
  19. {
  20. int arr[] = { 2, 3, 3, 5, 0 };
  21. int n = arr.length;
  22.  
  23. ReplaceElements(arr, n);
  24.  
  25. // Print the modified array.
  26. for (int i = 0; i < n; ++i) {
  27. System.out.print(arr[i]+" ");
  28. }
  29. System.out.println("");
  30.  
  31. }
  32. }
  33. // This code is contributed by mits
Runtime error #stdin #stdout #stderr 0.06s 32524KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at GFG.ReplaceElements(Main.java:14)
	at GFG.main(Main.java:23)