Home » C programs » C programs on Bitwise Operators

C program to find Binary number of a Decimal number

Get Binary values of an input number in C: In this C program, we will read an integer (decimal) number and print its Binary values (Binary 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).

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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.