Convert Decimal to Binary in C

Here you will get the program to change over decimal to binary in C.

We can change over a decimal number into binary by more than once isolating it by 2 and putting away the rest of.

Presently show the leftovers in invert request.

Program

#include<stdio.h>
 
int main()
{
	int d,n,i,j,a[50];
	printf("Enter a number:");
	scanf("%d",&n);
	
	if(n==0)
		printf("\nThe binary conversion of 0 is 0");
	else
	{
		printf("\nThe binary conversion of %d is 1",n);
		
		for(i=1;n!=1;++i)
		{
			d=n%2;
			a[i]=d;
			n=n/2;
		}
		
		for(j=i-1;j>0;--j)
			printf("%d",a[j]);
	}
 
	return 0;
}

Output

Enter a number:10

The binary conversion of 10 is 1010

Leave a Comment

error: Alert: Content is protected!!