Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A question in flipping a number

  1. Mar 19, 2008 #1
    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 (Text):

    public class binar {

         * @param args
        public static void main(String[] args) {
            // TODO Auto-generated method stub
        public static void binary(int n){
    public static void binar(int n,String str){
        if (n==0){

  2. jcsd
  3. Mar 19, 2008 #2


    User Avatar
    Science Advisor
    Homework Helper

    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.
  4. Mar 19, 2008 #3
    how can i reverse the order??
  5. Mar 19, 2008 #4


    User Avatar
    Science Advisor
    Homework Helper

    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.
  6. Mar 19, 2008 #5
  7. Mar 20, 2008 #6


    User Avatar

    If you use Java, you can just use its built-in function to do so:
    Code (Text):

    int fromBase = 10;
    int toBase = 2;

    Integer.toString (Integer.parseInt (args [0], fromBase), toBase);
    Next, you can just run your program conveniently like:
    Code (Text):

    java BaseNToMConverter 82733
    Best regards,
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook