Reputation: 5
I wrote this program to convert decimal numbers to another bases but when I run it with eclipse the answer is 0
for any number.
import java.util.Scanner;
public class dtb {
public static void main(String[] args) {
Scanner myscanner = new Scanner (System.in);
int num= myscanner.nextInt();
int base= myscanner.nextInt();
int i=0;
int y=0;
while (num >= base){
int x = (num%base);
num = num/base;
y = (y + (x*(10^i)));
}
System.out.println (y) ;
}
}
Upvotes: 0
Views: 84
Reputation: 34146
The ^
operator doesn't do what you think. If you want to elevate to a power, use Math.pow()
:
Math.pow(10, i)
but since this method returns a double
, you will have to cast it to int
:
(int) Math.pow(10, i)
Upvotes: 1
Reputation: 4411
Check your input. you num value should be greater than base value. eg: 30,20 output is 100.
Upvotes: 0