A question in flipping a number

  • Thread starter transgalactic
  • Start date
In summary, the conversation revolved around building a program that converts a number from decimal basis to binary basis. The main issue was that the resulting binary number was reversed. The solution was to reverse the order of appending the digits in the code. It was also mentioned that Java has a built-in function for converting bases.
  • #1
transgalactic
1,395
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));
}
}
 
Technology news on Phys.org
  • #2
That's pretty inefficient there!

In any case, your problem is that str is the end of your string, not the start. Reverse the order that you're appending them.
 
  • #3
how can i reverse the order??
 
  • #4
Look at the last line of your code (other than the two }s), it should be pretty obvious. You're sticking it on the wrong end.
 
  • #5
thanks
 
  • #6
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
 

1. What is "flipping a number"?

"Flipping a number" refers to the process of reversing the digits of a given number. For example, flipping the number 123 would result in the number 321.

2. Why is it important to flip a number?

Flipping a number can be useful in various mathematical calculations, such as finding the reverse of a number or determining its factors. It can also be used in coding and data analysis to manipulate numerical data.

3. How do you flip a number?

To flip a number, the digits need to be reversed in their order. This can be done manually by writing down the number and then writing it in reverse order. In coding, there are various methods and algorithms that can be used to flip a number.

4. Can any number be flipped?

Yes, any positive or negative integer can be flipped. However, numbers with leading zeros (e.g. 012) may need to be handled differently depending on the context.

5. Are there any practical applications for flipping numbers?

Aside from mathematical and coding purposes, flipping numbers can also be used in games or puzzles, such as finding hidden words or codes. It can also be a fun way to challenge one's mental math skills.

Similar threads

  • Programming and Computer Science
Replies
3
Views
770
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
871
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
747
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top