Java Program for Binary to Decimal Conversion | Study Table

 import java.util.Scanner;

public class BinaryToDecimal
{
    public static int dec, q, rem;
    public static StringBuffer sb;
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a Decimal Value: ");
        dec = scanner.nextInt();
        sb = new StringBuffer(" ");
        q = dec; rem = 0;
        BinaryToDecimal object = new BinaryToDecimal();
        System.out.println("The Binary form is "+(object.Binary(dec)).reverse());
        scanner.close();
    }

    StringBuffer Binary(int dec)
    {
        dec = dec/2; rem = q%2;
        sb.append(Integer.toString(rem));
        q = dec;
        if(q>0) Binary(q);
        else return sb;
        return sb;
    }
}

Comments

Popular posts from this blog

You have accidentally left your suitcase behind when you got off the train. You only realized it after the train left the platform. Write a letter to the station master reporting your loss and request that the suitcase is located and kept till you claim it.

You went to an ATM to withdraw some money but the machine did not dispense cash. However, the amount got debited from your account. Write a letter to the manager of a bank, complaining about the deduction of money from your Savings Bank Account, while you did not receive the cash from the ATM, which you used to withdraw the money | Study Table

Shorthand Assignments Program for Java