Question

In: Computer Science

1.Write a Java program that inputs a binary number and displays the same number in decimal....

1.Write a Java program that inputs a binary number and displays the same number in decimal.

2.Write Java program that inputs a decimal number and displays the same number in binary.

Solutions

Expert Solution

1. Write a Java program that inputs a binary number and displays the same number in decimal.

Code:

import java.util.*;
class binaryToDecimal
{
static int binaryToDecimal(int n)
{
int num=n;
int decimalValue=0;
int base=1;
int temp=num;
while(temp>0)
{
int last_digit=temp%10;
temp=temp/10;
decimalValue=decimalValue+last_digit*base;
base=base*2;
}
return decimalValue;
}
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter binary number to be converted to decimal: ");
int num=sc.nextInt();
System.out.print("Decimal number is: ");
System.out.println(binaryToDecimal(num));
}
}


Code Screesnhot:

Output Screenshot:

2. Write Java program that inputs a decimal number and displays the same number in binary.

Code:

import java.util.*;
class decimalToBinary
{
static void decimalToBinary(int n)
{
int[] binaryNum=new int[1000];
int i=0;
while(n>0)
{
binaryNum[i]=n%2;
n=n/2;
i++;
}
for(int j=i-1;j>=0;j--)
System.out.print(binaryNum[j]);
}
public static void main (String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter decimal number to be converted to binary: ");
int num=sc.nextInt();
System.out.print("Binary number is: ");
decimalToBinary(num);
}
}

Code Screesnhot:

Output Screenshot:


Related Solutions

Write a Java application that inputs an unknown number of employees and determines and displays the...
Write a Java application that inputs an unknown number of employees and determines and displays the gross pay for each employ. The company pays straight time for the first 40 hours worked by each employee (hours times rate), and straight time plus time-and-a-half for all hours worked in excess of 40 hours. Input the number of hours worked and hourly rate for each of the employees, then determine and display the employee’s gross pay. At the end of the program,...
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
Write a Java program that reads a whole number on the keyboard and obtains and displays...
Write a Java program that reads a whole number on the keyboard and obtains and displays twice and triple that number on the screen
Write a program that inputs a string that represents a binary number. The string can contain...
Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console. Examples of...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT