Problem:
1. You are given a decimal number n.Input Format
2. You are given a base b.
3. You are required to convert the number n into its corresponding value in base b.
A number nOutput Format
A base b
A number representing corresponding value of n in number system of base b
Constraints
0 <= d <= 512
2 <= b <= 10
Sample Input
57
2
Sample Output
111001
Solution:
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 dn = getValueInBase(n, b);
System.out.println(dn);
}
public static int getValueInBase(int n, int b){
// write code here
int pow = 1,newNo = 0;
while(n>0){
newNo += (n%b) * (pow);
pow *= 10;
n/=b;
}
return newNo;
}
}
Comments
Post a Comment