Conversion program from decimal to binary

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:

  1. Get the input number from the user.
  2. Take modulus of the number and stores into the array.
  3. Divide the number by two.
  4. Repeat step 2 and 3 until number becomes zero.
  5. 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();
}

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x