Java Program to Convert a Decimal Number to Binary & Count the Number of 1s
Last Updated :
19 Sep, 2022
As per the number system, default computations are carried over decimal numbers whose base is standardized as 10. Machine computes all the execution at the physical layer in 0s and 1s. So arises a need for a number system with base 2 known as a binary number system. A binary number can be converted to a decimal number and vice versa. In java there are 4 types of numbers:
Types Of Numbers | Base |
---|
Binary | 2 |
Octa-Decimal | 8 |
Decimal | 10 |
Hexadecimal | 16 |
Approaches: In Java, there are 2 ways to convert either using pre-defined methods or third-grade logic building as listed below:
- Using Integer.toBinaryString method (Integer wrapper class)
- Using the Brute force method (without any use of predefined classes)
Examples:
Input. 1: 10
Output 1: The binary equivalent of 10 is : 1010
Number of 1s is : 2
Input 2: 15
Output 2: The binary equivalent of 15 is : 1111
Number of 1s is 4
Approach 1: Using toBinaryString() Method: represents the number to be converted into binary. The Integer class of java provides some useful methods to deal with Integers. One such method is Integer.toBinaryString(int x)
Syntax:
public static String toBinaryString(int variable_name)
Parameters: Decimal integer to be converted.
Return Type: String which is holding binary representation of the integer converted or simply binary equivalent of integer as a String object.
Exceptions: There are no exceptions thrown by this method
Parameter: It takes one parameter of type Integer (or int)
Implementation: To count the number of 1s, check if each character of the obtained binary string equals 1 or not.
Java
// Java program to convert decimal to binary number
// and counting number of 1's in it
public class GFG {
// Function to convert decimal to binary
void convertAndCount(int num)
{
// Store the count of 1s currently 0
int count = 0;
// Returns a String object representing
// binary equivalent of passed decimal number
String binary = Integer.toBinaryString(num);
// Iterating over obtained string using length
// function
for (int i = 0; i < binary.length(); i++)
// Checking 1 is present in binary
if (binary.charAt(i) == '1')
// Increment the count
// if any char equals 1
count++;
// Printing the binary equivalent
System.out.println("The binary equivalent of " + num
+ " is : " + binary);
// Printing the no of 1 in above binary number
System.out.println("Number of 1s is : " + count);
}
// Main driver method
public static void main(String[] args)
{
// Creating object in main
GFG obj = new GFG();
// Calling the convertAndCount() method
// over the integer value 18
obj.convertAndCount(18);
}
}
OutputThe binary equivalent of 18 is : 10010
Number of 1s is : 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach 2:Without using pre-defined variables
- Dividing the decimal number by 2 which is to be converted into binary
- Storing the remainder
Java
// Java program to convert decimal to binary number
// and counting number of 1's in it
public class GFG {
// Function to convert decimal to binary
void convertAndCount(int num)
{
int temp = num; // a temporary variable to store the
// value of num
// to store the binary value
int[] binary = new int[20];
// to store the count of 1s
int count = 0;
int i;
// to iterate through the loop and keep a
// count of no.of digits in the obtained binary
for (i = 0; temp > 0; i++) {
// divide the number by 2
// and store the remainder
temp /= 2;
binary[i] = temp % 2;
// If 1 is present in binary
if (binary[i] == 1)
// increment the count if any digit
// is equal to 1
count++;
}
// Printing binary of decimal number
System.out.print("The binary equivalent of " + num
+ " is : ");
// Iterating over array
for (int j = i - 1; j >= 0; j--)
// Printing obtained array in reverse order
System.out.print(binary[j]);
// Printing number of 1's
System.out.println("\nNumber of 1s is : " + count);
}
// Main driver method
public static void main(String[] args)
{
// Creating class GFG object in main
GFG obj = new GFG();
obj.convertAndCount(18);
// calling convertAndCount() method on decimal
// over the value 18
}
}
OutputThe binary equivalent of 18 is : 01001
Number of 1s is : 2
Time complexity: O(n) for given number n
Auxiliary space: O(1)
Similar Reads
Java Program to Convert a Decimal Number to Binary Number using Stacks Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek
3 min read
Java Program to Convert a Decimal Number to Binary Number using Arrays as Stacks Given an Integer number convert into Binary Number using arrays as a stack. Example: Input : 10 Output: 1010 Input : 16 Output: 10000 Approach: Divide the number by 2 and store the remainder of the number in the array.Divide the number by 2.Repeat the process until the number becomes zero.Print the
1 min read
Java Program to Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15.
6 min read
Java Program to Count 1's in a sorted binary array Given a binary array sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = {1, 1, 0, 0, 0, 0, 0} Output: 2 Input: arr[] = {1, 1, 1, 1, 1, 1, 1} Output: 7 Input: arr[] = {0, 0, 0, 0, 0, 0, 0} Output: 0 A simple solution is to linearly traverse the array. The time c
3 min read
Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc
5 min read