A question in flipping a number

  • Thread starter Thread starter transgalactic
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a programming challenge related to converting a decimal number to its binary representation. Participants explore issues with the implementation and suggest potential solutions or alternatives, focusing on coding techniques and efficiency.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their implementation of a program to convert a decimal number to binary but notes that the output is reversed.
  • Another participant points out the inefficiency of the current method and suggests reversing the order of string concatenation to correct the output.
  • A participant asks for clarification on how to reverse the order of the string concatenation.
  • Further clarification is provided by another participant, indicating that the issue lies in appending to the end of the string rather than the beginning.
  • Another participant offers an alternative approach using Java's built-in functions for base conversion, suggesting a more efficient method.

Areas of Agreement / Disagreement

Participants express differing views on the efficiency of the original method and the best way to correct the output. There is no consensus on a single solution, as alternative methods are proposed.

Contextual Notes

Some limitations include the potential inefficiency of string manipulation in the original code and the lack of clarity on how to implement the suggested changes effectively.

Who May Find This Useful

Individuals interested in programming, particularly in Java, and those looking to understand number base conversions may find this discussion relevant.

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));
}
}
 
Technology news on Phys.org
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.
 
how can i reverse the order??
 
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.
 
thanks
 
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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K