Conversion program from decimal to binary
Question:
Write a program to convert the given number from decimal to binary.
Logic:
- The number for conversion is getting from the user.
- Modulus value of the number gives binary value.
- Then divide the number by 2.
- And then print the binary value.
Algorithm:
- Get the input number from the user.
- Take modulus of the number and stores into the array.
- Divide the number by two.
- Repeat step 2 and 3 until number becomes zero.
- Then print the binary number.
Program:
#include<stdio.h> void main() { int a[20],i=0,n,c; clrscr(); printf("enter the number:"); scanf("%d",&n); while(n>0) { a[i]=n%2; //stores the remainder values as o's and 1's n=n/2; //divide the number into half i++; //increments the index value }c=i; //length of the binary number for(i=c-1;i>=0;i--) //prints the binary numbers { printf("%d",a[i]); } getch(); }