2

I have written a java code to convert decimal number to binary.Its works well but when input is 1024 or more output goes wrong.Ex: if input is 1024 output is 1410065408.

    import java.util.Scanner;
    public class story {
        public static void main(String args[])
        {
            Scanner scan=new Scanner(System.in);
            System.out.println("Enter a positive decimal number");
            binary bin=new binary();
            System.out.println(bin.calculate(scan.nextInt()));
        }
    }

    public class binary {
         public long calculate(int num) 
        {
            int i=1;
            long result=0;
            while(num>0)
            {
                result=result+(num%2)*i;
                i*=10;
                num/=2;
            }
            return result;
       }

    }
  • 3
    You should change the datatype of `i` from `int` to `long`. – user2004685 Jan 06 '17 at 20:25
  • 2
    All numbers in Java are binary. What you are doing is converting from a decimal *representation* of a number to a binary representation of the *same* number, but first converting the original number to an entirely different number, then printing the decimal representation of a different number. What you should do is convert directly from the number itself, which is independent of representation, to a `String`. – Lew Bloch Jan 06 '17 at 21:12

3 Answers3

0

You are facing this issue because the datatype of i is int which is Overflowing.

The solution is to simply change the datatype of i from int to long.

Here is the corrected code snippet:

public static final class binary {
    public long calculate(int num) {
        long i = 1; //Note the change at this line
        long result = 0;
        while (num > 0) {
            result = result + (num % 2) * i;
            i *= 10;
            num /= 2;
        }
        return result;
    }
}

Input:

1024

Output:

10000000000
user2004685
  • 9,548
  • 5
  • 37
  • 54
0

Error is in type i, it should be long.

CODE:

import java.util.Scanner;
    public class story {
        public static void main(String args[])
        {
            Scanner scan=new Scanner(System.in);
            System.out.println("Enter a positive decimal number");
            System.out.println(calculate(scan.nextInt()));
        }


            public static long calculate(int num) 
           {
               long i=1;
               long result=0;
               while(num>0)
               {
                   result=result+(num%2)*i;
                   i*=10;
                   num/=2;
               }
               return result;
          }
}

OUTPUT:

Enter a positive decimal number
1026
10000000010
Dani
  • 1,825
  • 2
  • 15
  • 29
  • Is there any point in posting the exact same answer again? I have said the same thing in my answer below. You can just upvote it if you think it's correct. – user2004685 Jan 06 '17 at 20:29
  • It's alright. Please remember that it's more important to post quality answers and not the redundant content. Have a nice day ahead. – user2004685 Jan 06 '17 at 20:32
0

The "i" variable goes past maximum integer value for inputs 1024 and above.. Refer here : Why 10000000*1000 giving 1410065408 in java?

Community
  • 1
  • 1
sharpcodes
  • 143
  • 1
  • 11