how to convert binary to decimal and decimal to binary in C
lanaguage

Answer Posted / rahul

//it is to convert Binary to decimal;


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int a[20],i,n,count=0,b[20],c[20],sum=0;
printf("ENter the number in binary form=\t");
scanf("%ld",&n); // Get a binary number
from the user
for (i=0;n>=1;i++)
{
a[i]=n%10;
n=n/10; // Loop To reverse the number And put
all reversed numbers in arry a[i]
count=count + 1; // count to count the number of times
this loop runs
}
for (i=0;i<=count-1;i++) // count -1 condition is used to
run the loop till the previous loop run
{
b[i]=pow(2,i); // This is to raise the power of 2 to no
of times previous loop runned.
}
for (i=0;i<=count-1;i++)
{
c[i]=a[i] * b[i]; // Multiply a[i] or reveresed binary
no with b[i] or increasing pow of 2 to count-1
sum=sum +c[i]; // it is to add the c[i] elements with
each other n put into sum variable.
}
printf("Decimal form =%ld",sum); // printing the sum to get
the decimal form
getch();
}
// Hope it solves your problem, ANy more assistance
honey.gupta13@yahoo.com

Is This Answer Correct ?    15 Yes 9 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a pointer variable in c language?

641


What are file streams?

562


How are Structure passing and returning implemented by the complier?

707


Is it possible to use curly brackets ({}) to enclose single line code in c program?

787


What is null in c?

594






count = 0; for (i = 1;i < = 10; i++);count = count + i; Value of count after execution of the above statements will be a) 0 b) 11 c) 55 d) array

673


What is wild pointer in c with example?

568


What is the most efficient way to count the number of bits which are set in an integer?

584


Explain the use of 'auto' keyword in c programming?

677


What is actual argument?

585


Explain how can I read and write comma-delimited text?

646


Write a program to check armstrong number in c?

632


An expression to whose value an operater is applied a) operand b) variable c) constant d) all of the above

652


What are Macros? What are its advantages and disadvantages?

637


How can I copy just a portion of a string?

811