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

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 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 1. Write an application that inputs a telephone number as a string in the form...
JAVA 1. Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed....
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT