C program to convert decimal to binary without using array
C program to convert decimal to binary without using array
In the world of computer programming, converting numbers from one numeral system to another is a common task. One such conversion is converting decimal numbers to their binary equivalent. The binary numeral system is a fundamental representation of numbers in computer science.
In this article, we will explore how to convert decimal numbers to binary using the C programming language. However, we will take a unique approach and demonstrate how to achieve this without using arrays. By leveraging the power of bitwise operations and iterative processes, we can accomplish the conversion efficiently.
Understanding Decimal and Binary Systems:
Before we dive into the implementation, let's quickly review the decimal and binary numeral systems. The decimal system, also known as the base-10 system, is the most commonly used numeral system in our daily lives. It uses ten digits from 0 to 9 and relies on place value, where each digit's position represents a power of 10.
On the other hand, the binary system, or the base-2 system, is fundamental in computer science. It uses only two digits, 0 and 1, and each digit's position represents a power of 2. Converting a decimal number to its binary equivalent requires expressing the number as a sum of powers of 2.
Approach and Algorithm:
To convert decimal to binary without using arrays, we can employ a bitwise shifting technique. Here's an outline of the algorithm:
Take the input decimal number from the user.
Initialize an empty variable, let's call it "binary," to store the binary representation.
Set a mask value initially as 1 (i.e., mask = 1).
Iterate until the decimal number becomes zero.
Inside the loop:
a. Check if the least significant bit of the decimal number is set by performing a bitwise AND operation with the mask.
b. If the result is non-zero, append '1' to the binary representation.
c. If the result is zero, append '0' to the binary representation.
d. Right shift the decimal number by one bit.
Finally, reverse the binary representation to obtain the correct binary equivalent.
Implementation in C:
Now, let's implement the above algorithm in the C programming language:
#include <stdio.h>
unsigned int decimalToBinary(int decimal) {
unsigned int binary = 0;
unsigned int mask = 1;
while (decimal > 0) {
if ((decimal & 1) == 1) { // Check if the least significant bit is set
binary = binary * 10 + 1;
} else {
binary = binary * 10;
}
decimal >>= 1; // Right shift decimal by one bit
}
// Reversing the binary representation
unsigned int reversedBinary = 0;
while (binary > 0) {
reversedBinary = reversedBinary * 10 + (binary % 10);
binary /= 10;
}
return reversedBinary;
}
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
unsigned int binary = decimalToBinary(decimal);
printf("Binary equivalent: %u\n", binary);
return 0;
}
Output:
![C program to convert decimal to binary without using array](https://static.tutorialandexample.com/c/c-program-to-convert-decimal-to-binary-without-using-array1.png)
In the above output, the program prompts the user to enter a decimal number. The user inputs 25. The program then calculates the binary equivalent using the provided algorithm and displays the result. In this case, the decimal number 25 is converted to its binary equivalent, which is 11001.
In this code implementation, we first define the decimalToBinary function that takes an integer decimal as input and returns an unsigned integer representing the binary equivalent. The function uses a while loop to iterate until the decimal number becomes zero.
Inside the loop, we check the least significant bit of the decimal number using a bitwise AND operation with the mask (1). If the result is non-zero, we append '1' to the binary representation; otherwise, we append '0'. We then right-shift the decimal number by one bit.
After the loop, we reverse the binary representation by extracting each digit and building the reversed binary number.
In the main function, we take user input for the decimal number, call the decimalToBinary function, and display the binary equivalent.
Conclusion:
In this article, we explored a C program to convert decimal numbers to their binary equivalent without using arrays. By leveraging bitwise shifting and iterative processes, we achieved an efficient conversion process. Understanding the decimal and binary numeral systems is crucial for any programmer, as it provides a foundation for working with numerical data in the binary format. Remember, arrays can be useful in many scenarios, but in situations where they are not required, we can rely on other techniques to accomplish our goals.