×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

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).

Problem statement

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

Finding Binary number of a Decimal number

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

C Bitwise Operators Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.