Q:

C program to find Binary number of a Decimal number

0

C program to find Binary number of a Decimal number

Given an integer number and we have to find/print its binary value using C program.

In this program, we are finding the Binary values of 16 bits numbers, the logic is very simple – we have to just traverse each bits using Bitwise AND operator. To traverse each bit – we will run loop from 15 to 0 (we are doing this to print Binary in a proper format).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Binary from Decimal (Integer) number using C program

#include <stdio.h>

/*function declaration
	* name		: getBinary
	* Desc		: to get binary value of decimal number
	* Parameter	: int -integer number
	* return	: void
*/
void getBinary(int);

int main()
{
	int num=0;
	printf("Enter an integer number :");
	scanf("%d",&num);
	printf("\nBinary value of %d is =",num);
	getBinary(num);
	return 0;
}

/*Function definition : getBinary()*/
void getBinary(int n)
{
	int loop;
	/*loop=15 , for 16 bits value, 15th bit to 0th bit*/
	for(loop=15; loop>=0; loop--)
	{
		if( (1 << loop) & n)
			printf("1");
		else
			printf("0");
	}
}

Output

Enter an integer number :13
Binary value of 13 is =0000000000001101

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C solved programs/examples on Bitwise Operators

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to demonstrate example of left shift (&l... >>