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...

Capturing Screen Shot using java

Here i will tell you how to write a program using java which is capable of capturing screenshots(either whole or partial).And save it as an image. Which java API ? The java.awt.Robot class provides a useful method for capturing a screenshot.Here is the method prototype. BufferedImage createScreenCapture(Rectangle screenRect) It is clear from the prototype that this function returns a BufferedImage and takes a Rectangle class object (portion of the screen to capture) as the argument.The BufferedImage which is returned could be saved using ImageIO class write method.Lets begin with the program. 1) Capturing full screen In order to capture full screen using  createScreenCapture(Rectangle screenRect) screenRect should have the full screen size and in order to do that we will use Toolkit class of java. Rectangle screenRect =  new Rectangle( Toolkit.getDefaultToolkit().getScreenSize() ); Now here begins the whole program. /* ******...

Hijack Clipboard using java

What is Clipboard? The clipboard is a special location in your computer's memory that temporarily stores data that has been cut or copied from a document. This data can then be pasted to a new location. The clipboard will typically hold its information until you cut or copy something else, or log out of the computer. For example, a user may copy information from a word processor and paste that information into an e-mail message. What is Clipboard Hijack? A clipboard hijacking is an exploit in which the attacker gains control of the victim'sclipboard and replaces its contents with their own data, such as a link to a malicious Web site.For more details see  here . Hacking clipboard using java. Our java code will repeatedly change clipboard text with the hackers text, may be a irritating line ,or a URL to your site. We will use the Toolkit class to get the system clipboard and then we will use the Clipboard class to change the content of clipboard. ...