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
```

TOP Interview Coding Problems/Challenges

Learn PCB Designing: PCB DESIGNING TUTORIAL

 Recommended posts C Tips & Tricks, C++ Tips & Tricks Introduction to Linux (Its modes, Safety, Most popular Applications) Linux Best Distros of 2018 C programming optimization techniques Differences b/w C & Embedded C? Embedded C Interview Q. & A. C programming tips for Embedded Development Basic rules of writing a C program Important points (rules) to remember while writing C/C++ program Top 5 Websites for solving programming challenges Read more...

 Others... Computer G.K. (MCQ) Most viewed pages... Categories...

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