Thursday, 12 July 2012

Program to convert Binary Number into Decimal Number in C


Sample Output - 





Code - 


/* Program to convert Binary number into Decimal number (No Validations Included)*/

//___ For Full Logic visit another post named "Convert Decimal to Binary/Binary to Decimal" ___

#include<stdio.h>

#include<conio.h>

#include<math.h> //__ Included for the pow(x,y) function __

/* To find out the implementation of the pow(x,y) function
check out post "One number raise to power another number in C" */

int main()
{
int decimal_number = 0, binary_number = 0, temp_number = 0, remainder = 0, counter = 0;

printf("\n\n\t   __ Program to convert Binary number into Decimal number __");

printf("\n\n\t\t\t __ No Validation Included __");

printf("\n\n\n  Enter the Binary Number - ");

scanf("%d",&binary_number); //__ Reading the binary number ___

temp_number = binary_number;

//__ Creating a copy of the binary_number inputted for diaplying purposes __

while(binary_number != 0)
{
remainder = binary_number % 10; //___ Extracting the digits ____

binary_number = binary_number / 10; //__ Creating a new number ____

decimal_number = decimal_number + remainder * pow(2,counter);
    
               //___ Forming the decimal number above __

counter++;
}

printf("\n\n  Decimal Conversion of Binary Number %d (BASE 2) is -- %d (BASE 10)", 

temp_number, decimal_number);

getch();

return 0;

/* return 0 tells the compiler that program has executed successfully without any errors */
}



No comments:

Post a Comment