Problem statement
Write a program to convert decimal number to binary number.
We can convert any decimal number (base-10 (0 to 9)) into binary number(base-2 (0 or 1)).
What is binary number and decimal number?
A binary number is consist of only 0’s and 1’s.
We can represent any number into the binary form.
Each digit is referred to as a bit.
Numbers from 2 to 9 are called as decimal numbers.
For example:
Decimal number | Binary number |
---|---|
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
In this way, we can convert any number into the binary number.
Program for converting decimal to binary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> int main() { int a[10],num,i; printf("Enter a decimal number from 0 to 9\n "); scanf("%d",&num); for(i=0; num>0; i++) { a[i] = num%2; num = num/2; } printf("Converted %d number to Binary Number is\t", num); for(i=i-1;i>=0;i--) { printf("%d",a[i]); } return 0; } |
Output:
1 2 3 |
Enter a decimal number from 0 to 9 9 Converted 0 number to Binary Number is 1001 |