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
What is anagram in c?
What is realloc in c?
Why do we use main function?
Explain how can you restore a redirected standard stream?
Multiply an Integer Number by 2 Without Using Multiplication Operator
what do you mean by enumeration constant?
What is sizeof int in c?
How can I trap or ignore keyboard interrupts like control-c?
What is the difference between printf and scanf in c?
What are the two types of structure?
What are the different types of data structures in c?
console I/O functions means a) the I/O operations done on disk b) the I/O operations done in all parts c) the input given through keyboard is displayed VDU screen d) none of the above
what is event driven software and what is procedural driven software?
1234554321 1234 4321 123 321 12 21 1 1 12 21 123 321 1234 4321 1234554321
A global variable when referred to in another file is declared as this a) local variable b) external variable c) constant d) pointers