Skip to main content

Decimal to binary and vice versa program in java


1)Decimal to binary conversion program

import java.util.Scanner;
public class Deci2Bin
{
    public static void main(String[] args) 
    {
        int n;
        String x;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter any decimal number:");
        n = s.nextInt();
       Deci2Bin obj = new Deci2Bin();
        x = obj.inBinary(n);
        System.out.println("Binary number:"+x);
    }
    String inBinary(int y)
    {
        int a;
        if(y > 0)
        {
            a = y % 2;
            return (binary(y / 2) + "" +a);
        }
        return "";
    }
}


2)Binary to decimal conversion Program


import java.util.Scanner;
public class BinToDeci {
 
    public int getDecimalFromBinary(int binary){
         
        int decimal = 0;
        int power = 0;
        while(true){
            if(binary == 0){
                break;
            } else {
                int tmp = binary%10;
                decimal += tmp*Math.pow(2, power);
                binary = binary/10;
                power++;
            }
        }
        return decimal;
    }
     
    public static void main(String a[]){
Scanner sc=new Scanner(System.in);       
System.out.print("Enter binary number(0and 1)");
int bin=sc.nextInt(); 
BinToDeci bd = new BinToDeci();
        System.out.print("Decimal Number:"+getDecimalFromBinary(bin);
    }
}

Comments

Popular posts from this blog

Create a simple virus using java

What's my idea Before we start talking how we will create a simple flooder virus using java we must be clear about what is  the idea and how to implement it. So the idea is to create a program which will create  thousands of blank files in a drive you will don't believe but it could create file with  a speed of   1060+ files per second . How to implement it Now the problem is how we would do this?? So here is the answer .  First of all we will design a loop which will run  N  number of times where  N  is the number of files which we want to create. Then we will create a new File with the help of  createNewFile()  function of  java.io.File  class. The name of each file is the value of the variable which is used in the loop. Here is the sample virus program. /**  * **********This virus program is just for educational purpose   * author will not be re...

How to setup a .jar file in Blue-J

How to set a new package in Blue-J In this tutorial you will learn how you can setup a jar file in Blue J IDE. Follow the given steps and you could easily set it up. Let me specify that in my case we will install a jar file created my me that is utility.jar .If you are downloading other packages like Free TTS,open cv, j3d or any other then you just need to find the jar files in their folder and rest of the tutorial remain the same. Step 1:- Open blue-J Ide Open your blue j ide in my case it is Blue-J 3.1.7. Step 2:- In the top menu bar click on TOOLS and after that in the drop down menu click on PREFERENCES . Step 3 :- After clicking on preferences you will see a screen like this.      Just click on Libraries Tab.   Step 4 :- After clicking on libraries blue j will show the list of pre loaded jar files that you must be using before.On the right hand side of the window you will see ADD button just click on it. Step 5:- ...

How to setup a .jar file in Eclipse IDE

How to set a new package in Eclipse IDE In this tutorial you will learn how you can setup a jar file in Eclipse IDE. Follow the given steps and you could easily set it up. Let me specify that in my case we will install a jar file created my me that is utility.jar .If you are downloading other packages like Free TTS,open cv, j3d or any other then you just need to find the jar files in their folder and rest of the tutorial remain the same. Step 1:- Open Eclipse Ide Open your Eclipse ide in my case it is  Version: Luna Service Release 2 (4.4.2) Step 2:- In the top menu bar click on  WINDOW  menu item. And then you will see a pop up menu will appear.In the pop up menu click on  PREFERENCES  . Step 3:- Then in the next window's left hand side click on JAVA and then on  BUILD PATH   .Under build path you will see two options   classpath variables   and  User Libraries   you need to click on user...