Vous êtes sur la page 1sur 6

(http://www.c4learn.

com/c-programs/)

Table of Content
C Program to Convert Decimal number into Binary Number
(http://www.c4learn.com/c-programs/program-to-convert-decimal-number-into.html)
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
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 Binary to Decimal number

Gmail for Work


Look more professional with
custom Gmail from Google
Start free trial

Program to Convert Binary to Decimal number:Number System

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

// Function Definition

{
long int rem,sum=0,power=0;
while(num>0)
{
rem = num%10;
num = num/10;
sum = sum + rem * pow(2,power);
power++;
}
printf("Decimal number : %d",sum);
}
//------------------------------------void main()
{
long int num;
clrscr();
printf("Enter the Binary number (0 and 1): ");
scanf("%ld",&num);
bin_dec(num);
getch();
}

Output :

Enter the Binary number : 111


Decimal number : 7

Note :
This program is for beginners (not for Experts) thats why we havent provided any
Validations while accepting the input. Please provide proper binary input to this program
i.e (0 and 1).

Logic of This Program :


In the main function we have accepted the proper binary input i.e suppose we have
accepted 111 as binary input.

printf("Enter the Binary number (0 and 1): ");


scanf("%ld",&num);

Inside the function while loop gets executed. Sample Dry run for the while loop is shown
below
Iteration

num

rem

sum

power

Before Entering into Loop

111

Garbage

After Iteration 1

11

After Iteration 2

After Iteration 3

Algorithm for Binary to Decimal :


1. Accept Number from User
2. Divide Number by 10 and Store Remainder in variable rem
3. Divide Original Number by 10.

sum = sum + rem * pow(2,power);

Inside the First Iteration power = 0. Power is Incremented in each Iteration.


4. Calculate sum using the above formula, calculated sum is nothing but the decimal
representation of the given binary number.

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

0 Comments


Like Page

Be the first of your friends to like this

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.

Vous aimerez peut-être aussi