You are on page 1of 6

(http://www.c4learn.

com/c-programs/)

Table of Content
C Program to Convert Decimal number into Binary Number
C Program to Convert Decimal number to Octal Number
(http://www.c4learn.com/c-programs/program-for-decimal-number-to-octal.html)
C Program to Convert Decimal Number to Hexadecimal Number
(http://www.c4learn.com/c-programs/program-for-decimal-to-hexadecimal.html)
C Program to Convert Binary to Decimal number
(http://www.c4learn.com/c-programs/program-to-convert-binary-to-decimal.html)
C Program to Convert Decimal to Binary using Bitwise AND operator
(http://www.c4learn.com/c-programs/decimal-to-binary-using-bitwise-and.html)

C Program to Convert Decimal number into Binary


Number

COMPOSE

Gmail for Work

Program to Convert Decimal number into Binary : Number System :


Download

#include<stdio.h>
#include<conio.h>
#include<math.h>
void dec_bin(long int num)

// Function Definition

{
long int rem[50],i=0,length=0;
while(num>0)
{
rem[i]=num%2;
num=num/2;
i++;
length++;
}
printf("nBinary number : ");
for(i=length-1;i>=0;i--)
printf("%ld",rem[i]);
}
//================================================
void main()
{
long int num;
clrscr();
printf("Enter the decimal number : ");
scanf("%ld",&num);
dec_bin(num);

// Calling function

getch();
}

Output :

Enter the decimal number : 7


Octal number : 111

Explanation of Program :
Always Program execution starts from main function. Inside Main function we are
accepting the decimal number.

printf("Enter the decimal number : ");


scanf("%ld",&num);

Now after accepting the input decimal number we pass the decimal number to function
dec_bin(num) function using following syntax.

dec_bin(num);

// Calling function

Inside the Function Call


We have declared some variables and the use of each variable is listed in following table
Variable

Data Type

Initial Value

Use of Variable

rem[50]

long int

Garbage

Storing the Remainder

long int

Subscript Variable for Running Loop

length

long int

Storing the Length of the Array

We are following steps until the number becomes 0 or less than 0


[box]Step 1 : Check Whether the Number is Less than or Equal to Zero [/box]
[box]Step 2 : Divide the number by 2 and store the remainder in the array [/box]
while(num>0)
{
rem[i]=num%2;
num=num/2;
i++;
length++;
}

[box]Step 3 : Increase the length of the array by 1 and also increment the subscript
variable[/box]
After the execution of while loop print the array in reverse order.

Download Decimal to Binary Code :


[box]Download Code (http://www.box.net/shared/ph7afem3s4)[/box]

Official HP Online
Store
Buy HP Original Toner
Cartridges. Free Same Day
Delivery. Pay COD.

Get in Touch!
Recent Programs
C Program to read the content of file using fgets (http://www.c4learn.com/c-programs/readcontent-file-using-fgets.html)
C Program to perform arithmetic operations on float
(http://www.c4learn.com/c-programs/perform-arithmetic-operations-on-float.html)

C Program to perform arithmetic operations on integer


(http://www.c4learn.com/c-programs/perform-arithmetic-operations-integers.html)
C Program to count trailing zeros using bitwise operator
(http://www.c4learn.com/c-programs/count-trailing-zeros-using-bitwise-operator.html)
C Program to convert number to binary using bitwise operators
(http://www.c4learn.com/c-programs/convert-number-to-binary-using-bitwise-operators.html)

Copyright 2015. All Rights Reserved.

You might also like