You're online now.

Hurray! you are online now.

# C program to convert decimal to binary without array

We can convert any decimal number into binary number in c without using an array.

## Decimal Number

A Decimal number is a base 10 of number because it ranges goes from 0 to 9, in decimal number there are total 10 digits between 0 to 9. Any combination of digits is decimal number. For Example: 33, 782, 7, 0, 5, etc.

## Binary Number

A binary number is a base 2 of number because it ranges goes from 0 to 1, it has only two digits 0 and 1. Any combination of digits is binary number. For Example: 0110001, 111001, 101, etc.

## Algorithm to convert decimal to binary

1. Run the loop while n is greater than 0.
2. Calculate the remainder and store the value in j variable, after this initialized, a variable temp which store the power of 10 after every iteration.
3. bin += j * temp → here we store the new bit in bin variable here bin variable store the binary of the digit.
4. after perform these operation than remove the last bit of the number using n = n / 2.
5. Exit to the loop.
6. print the binary of the digit given by user.
7. Exit the program.

### Convert decimal to binary using for loop

``````#include <stdio.h>
#include <math.h>
int main() {
int n, i, j, bin = 0, temp;
printf("Enter the Decimal Number : ");
scanf("%d", &n);
temp = n;
for(i = 0; n > 0; i++){
j = n % 2;
int temp = pow(10, i);
bin += j * temp;
n = n / 2;
}
printf("\nBinary of %d is : %d", temp, bin);

return 0;
}

/*

Enter the Decimal Number : 34
Binary of 34 is : 100010

*/``````

### Convert decimal to binary using while loop

``````#include <stdio.h>
#include <math.h>
int main() {
int n, i = 0, bin = 0, temp;
printf("Enter the Decimal Number : ");
scanf("%d", &n);
temp = n;
while(n > 0){
int temp = pow(10, i);
bin += (n % 2) * temp;
n = n / 2;
i++;
}
printf("\nBinary of %d is : %d", temp, bin);

return 0;
}

/*

Enter the Decimal Number : 34
Binary of 34 is : 100010

*/``````
🖤 0