Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //elab-source: NumberBaseConverter.java
- import java.util.Scanner;
- public class NumberBaseConverter {
- public static void main(String [] args) {
- Scanner input = new Scanner(System.in);
- int ans = 0,counter = 0,count = 0;
- System.out.print("Input B = ");
- int b = input.nextInt();
- System.out.printf("Input number in base %d : ",b);
- int numInBase = input.nextInt();
- String Base = b+"";
- count = Integer.toString(numInBase).length()-1;
- while (count>=0){
- ans += Math.pow(b,count)*Integer.parseInt(Integer.toString(numInBase).substring(counter, counter+1));
- counter++;
- count--;
- }
- System.out.printf("%d base %d is equal to %d.",numInBase,b,ans);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement