thespeedracer38

DecimalToBinary

Mar 10th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.47 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class DecimalToBinary
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         Scanner sc = new Scanner(System.in);
  7.         System.out.print("Enter a number (base 10): ");
  8.         int num = sc.nextInt();
  9.         int temp = num;
  10.         String bin = "";
  11.         while(temp > 0)
  12.         {
  13.             bin = (temp % 2) + bin;
  14.             temp /= 2;
  15.         }
  16.         System.out.println("The binary for " + num + " is " + bin);
  17.     }
  18. }
Add Comment
Please, Sign In to add comment