Computer Applications
Write a program to input a number and display it into its Binary equivalent.
Sample Input: (21)10
Sample Output: (10101)2
Java
Java Iterative Stmts
29 Likes
Answer
import java.util.*;
public class KboatDecimaltoBinary
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the decimal number: ");
long decimal = in.nextLong();
long binary = 0, m = 1, rem = 0;
while(decimal > 0) {
rem = decimal % 2;
binary = binary + (rem * m);
m *= 10;
decimal /= 2;
}
System.out.println("Equivalent binary number: " + binary);
}
}
Variable Description Table
Program Explanation
Output
Answered By
10 Likes
Related Questions
A computerised bus charges fare from each of its passengers based on the distance travelled as per the tariff given below:
Distance (in km) Charges First 5 km ₹80 Next 10 km ₹10/km More than 15 km ₹8/km As the passenger enters the bus, the computer prompts 'Enter distance you intend to travel'. On entering the distance, it prints his ticket and the control goes back for the next passenger. At the end of journey, the computer prints the following:
- the number of passenger travelled
- total fare received
Write a program to perform the above task.
[Hint: Perform the task based on user controlled loop]A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, then display the message "Special two—digit number" otherwise, display the message "Not a special two-digit number".
A number is said to be Duck if the digit zero is (0) present in it. Write a program to accept a number and check whether the number is Duck or not. The program displays the message accordingly. (The number must not begin with zero)
Sample Input: 5063
Sample Output: It is a Duck number.
Sample Input: 7453
Sample Output: It is not a Duck number.Write a program to input a number and check whether it is a prime number or not. If it is not a prime number then display the next number that is prime.
Sample Input: 14
Sample Output: 17