C Program to Convert Decimal to Binary

C Program to Convert Decimal to Binary

Write C Program to Convert Decimal to Binary

// Write C Program to Convert Decimal to Binary

#include <stdio.h>
#include <math.h>

int main()
{
	int n, B[100], j;
	int ans = 0;

	printf("Enter the Decimal number :--> "); //assume that we have entered 7
	scanf("%d", &n);

	int i = 0;

	while(n > 0)
	{
		B[i] = n % 2;			//to store the remainder in array
		n = n >> 1;				//Right shift the value of n
		i++;
	}

	printf("The Binary number is :--> ");
	for(j = i - 1; j >= 0; j--)
	{
		printf("%d",B[j]);
	}

	return 0;
}

Output:

Enter the Decimal number :--> 89
The Binary number is :--> 1011001