Question

In: Computer Science

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

Solutions

Expert Solution

Binary to Decimal Method:

    // function to convert binary to decimal
    static int binaryToDecimal(String bin)
    {
        String num = bin;
        int dec = 0, base = 1, len = num.length();
        for (int i = len - 1; i >= 0; i--) {
            if (num.charAt(i) == '1')
                dec += base;
            base = base * 2;
        }
        return dec;
    }

Decimal to binary Method:

    // function to convert decimal to binary
        static int decimalToBinary(int dec)  
        { 
                int bin = 0, cnt = 0;  
                while (dec != 0) 
                {  
                        int rem = dec % 2;  
                        double c = Math.pow(10, cnt);  
                        bin += rem * c;  
                        dec /= 2; 
                        cnt++;  
                }  
                return bin;  
        }

Testing above methods:

Code:

public class Main
{
    // function to convert binary to decimal
    static int binaryToDecimal(String bin)
    {
        String num = bin;
        int dec = 0, base = 1, len = num.length();
        for (int i = len - 1; i >= 0; i--) {
            if (num.charAt(i) == '1')
                dec += base;
            base = base * 2;
        }
        return dec;
    }
    // function to convert decimal to binary
        static int decimalToBinary(int dec)  
        { 
                int bin = 0, cnt = 0;  
                while (dec != 0) 
                {  
                        int rem = dec % 2;  
                        double c = Math.pow(10, cnt);  
                        bin += rem * c;  
                        dec /= 2; 
                        cnt++;  
                }  
                return bin;  
        }
        public static void main(String[] args) {
                System.out.println("Decimal Eqivalent of 110 = " + binaryToDecimal("110"));
                System.out.println("Binary Eqivalent of 6 = " + decimalToBinary(6));
        }
}

Output:

i hope it helps..

If you have any doubts please comment and please don't dislike.

PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME


Related Solutions

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.
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
Write a program that accepts a number of minutes and converts it to days and hours....
Write a program that accepts a number of minutes and converts it to days and hours. For example, 6000 minutes represents 4 days and 4 hours. Be sure to provide proper exception handling for non-numeric values and for negative values. Save the file as  MinuteConversionWithExceptionHandling.java
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary tree that contain odd numbers. You are essentially writing a method that will become part of the IntegerTree class. You may define private helper methods to solve this problem. Make your own trees in the main method of the code. I specifically kept vague the way the nodes are being added to the tree so you can practice building your own trees from scratch...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value. Your function should be named decimalToBinaryRecursive Your function should take a single argument An integer to be converted to binary Your function should not print anything Your function should use recursion instead of a loop. Note that if you try to use a...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); //...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); // converts a hexadecimal number (represented as a STRING) to decimal string dectobin(int); // converts a decimal number to binary (represted as a STRING) string dectohex(int); // converts a decimal number to hexadecimal (represted as a STRING) //the addbin and addhex functions work on UNsigned numbers string addbin(string, string); // adds two binary numbers together (represented as STRINGS) string addhex(string, string); // adds two hexadecimal...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name...
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name the function b = deciTObina (d), where the input argument d is the number to be converted and the output argument b is a 30-element-long vector with 1s and 0s that represents the number in binary form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of b store the digits to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT