Friday 19 August 2016

Chapter 5 Exercise 37, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**5.37 (Decimal to binary) Write a program that prompts the user to enter a decimal integer and displays its corresponding binary value. Don’t use Java’s Integer .toBinaryString(int) in this program. 
import java.util.Scanner;
 
public class ProgrammingEx5_37 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter an integer:");
  int d = input.nextInt();
  String b = "";
 
  while (d != 0) {
   b = d % 2 + b;
   d = d / 2;
  }
 
  System.out.println("The binary form is " + b);
 
 }
 
}

No comments :

Post a Comment