We can convert any decimal number into binary number in c without using an array.
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.
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.
Decimal | Binar |
---|---|
1 | 1 |
2 | 10 |
3 | 111 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
#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
*/
#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
*/
Comments
Oops!