I am trying to convert the no n
with base b
into corresponding value in the decimal no system. This code is giving the wrong output.
For example [1172]base 8 output should be [634] base 10 but it is giving 100 as output.
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int b = scn.nextInt();
int d = getValueIndecimal(n, b);
System.out.println(d);
}
public static int getValueIndecimal(int n, int b){
// write your code here
int k=0;
int result = 0;
while(n!=0)
{
int r = n % 10;
b = b^k;
result += r * b;
k = k+1;
n = n / 10;
}
return result;
}
}