i cannot fix "Exception in thread "main" java.lang.ArithmeticException: / by zero" [duplicate]

StackOverflow https://stackoverflow.com/questions/20166144

  •  04-08-2022
  •  | 
  •  

Question

This is my program. I cannot find anything wrong with it, but when im trying to run it there is an error saying im dividing by zero.

import acm.program.*;  
public class ReverseDigits extends Program {  
public void run(){
println("This program reverses the digits in an integer."); 
int n = readInt("Enter a positive integer: ");
int x = 10;  
int t = 1;
int total = 0;
 //Finds the number of digits
 while (n > 0){
    while (n % x != 0) {
        t = t + 1;
        x = x * 10;
}
}
//In case the number has one digit the new number is the same
if(t == 1) {
 total = n;
}
//Creating the new number
while (t > 1) {
      t=t-1;
      total = (total + ((( n / (10^t)) - ((n /  (10 ^ (t+1))) * 10 )) * 10));
     } 
  println("The reverse number is " + total); 
   }  
}

I'm getting this error while trying to run it but I cannot find the problem.

Exception in thread "main" java.lang.ArithmeticException: / by zero
Was it helpful?

Solution

^ - is a XOR opertion, not degree! You need Math.pow(number, degree);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top