(Please help me!) Write a Java program which will accept as input a double preci
ID: 3880428 • Letter: #
Question
(Please help me!) Write a Java program which will accept as input a double precision number and convert it to its binary representation, using stack structure for the integer part of the number and queue structure for its decimal part.
In particular the program will perform the following:
- input a double precision number from keyboard.
- input from keyboard the precision of the decimal part, i.e. the number of the binary digits that will be used for the representation of the decimal part of the number.
- calculate the equivalent binary representation of the number.
- show on screen the equivalent binary representation of the number as well as its representation as a sum of 2^N.
Thank you!
Explanation / Answer
/*
* Java Program to Convert a Decimal Number
* to Binary Number using Stacks
*/
import java.util.*;
/* DecimalToBinaryUsingStacks */
public class DecimalToBinaryUsingStacks
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating Stack object */
Stack<Integer> stk = new Stack<Integer>();
/* Accepting number */
System.out.println("Enter decimal number");
int num = scan.nextInt();
while (num != 0)
{
int d = num % 2;
stk.push(d);
num /= 2;
}
/* Print Binary equivalent */
System.out.print(" Binary equivalent = ");
while (!(stk.isEmpty() ))
{
System.out.print(stk.pop());
}
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.