How to use Math.pow() method (Without using Arrays) In Java

Decimal to binary conversion can also be done without using arrays. 

Java Program to Convert Decimal Number to Binary Using Math.pow() Method

Java




// Java implementation of the approach
import java.io.*;
  
class GFG {
  
    // Function to return the binary
    // equivalent of decimal value N
    static int decimalToBinary(int N)
    {
  
        // To store the binary number
        int B_Number = 0;
        int cnt = 0;
        while (N != 0) {
            int rem = N % 2;
            double c = Math.pow(10, cnt);
            B_Number += rem * c;
            N /= 2;
  
            // Count used to store exponent value
            cnt++;
        }
  
        return B_Number;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int N = 17;
          System.out.println("Decimal - " + N);
          System.out.print("Binary - ");
        System.out.println(decimalToBinary(N));
    }
}
  
// This code is contributed by ajit.


Output

Decimal - 17
Binary - 10001

The complexity of the above method

Time Complexity: O(log n)
Auxiliary Space: O(1)

Steps for Conversion

  1. Initialize a decimal number to 10.
  2. Call the decimalToBinary() method with the decimal number as the argument.
  3. Inside the decimalToBinary() method, initialize variables remainder, quotient, and binaryNum.
  4. While the quotient is greater than 0, do the following:
    a. Compute the remainder by taking the modulus of the quotient with 2.
    b. Append the remainder to the beginning of the binaryNum string.
    c. Update the quotient by dividing it by 2.
  5. Return the binaryNum string.
  6. Print the decimal number and the binary representation of the number.

Java




public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        int decimal = 10;
        String binary = decimalToBinary(decimal);
        System.out.println("Decimal: " + decimal);
        System.out.println("Binary: " + binary);
    }
  
    public static String decimalToBinary(int n)
    {
        int remainder, quotient = n;
        String binaryNum = "";
        while (quotient > 0) {
            remainder = quotient % 2;
            binaryNum
                = Integer.toString(remainder) + binaryNum;
            quotient = quotient / 2;
        }
        return binaryNum;
    }
}


Output

Decimal: 10
Binary: 1010

The complexity of the above method

Time Complexity: O(log n)
Auxiliary Space: O(log n)

Please refer complete article on Program for Decimal to Binary Conversion for more details!



Java Program for Decimal to Binary Conversion

Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent binary number.

Examples: 

Input : 7
Output : 111

Input: 33
Output: 100001

Binary-to-decimal conversion is done to convert a number given in the binary system to its equivalent in the decimal number system. A number system is a format to represent numbers in a certain way. 

Binary Number System – The binary number system is used in computers and electronic systems to represent data, and it consists of only two digits which are 0 and 1. 

Decimal Number System – The decimal number system is the most commonly used number system worldwide, which is easily understandable to people. It consists of digits from 0 to 9.

Similar Reads

Methods For Decimal to Binary Conversion

There are numerous approaches to converting the given decimal number into an equivalent binary number in Java. A few of them are listed below....

1. Using Arrays

Algorithm...

2. Using  Bitwise Operators

...

3. Using Math.pow() method (Without using Arrays)

We can use bitwise operators to do the above job....

Contact Us