A question in flipping a number

  • Thread starter Thread starter transgalactic
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 2K views
transgalactic
Messages
1,386
Reaction score
0
i was told to build a program that transforms a number from deximal basis
into binary basis

i have built it but i get the resolt reversed
for the number 4 i get 001 instead of 100
i know it happening because the last digit comes last

how to change this method so it will show me the right resolt??

Code:
public class binar {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
binary(4);
	}
	public static void binary(int n){
		binar(n,"");
	}
public static void binar(int n,String str){
	if (n==0){
		System.out.println(str);
	}
	else
		binar((int)Math.floor(n/2),str+(n%2));
}
}
 
Physics news on Phys.org
how can i reverse the order??
 
transgalactic said:
i was told to build a program that transforms a number from deximal basis
into binary basis

If you use Java, you can just use its built-in function to do so:
Code:
int fromBase = 10;
int toBase = 2;

Integer.toString (Integer.parseInt (args [0], fromBase), toBase);

Next, you can just run your program conveniently like:
Code:
java BaseNToMConverter 82733


Eus