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 are pointers really good for, anyway?

610


How can I swap two values without using a temporary?

610


What is structure of c program?

600


program for reversing a selected line word by word when multiple lines are given without using strrev

1941


What is difference between union and structure in c?

572






Why do we need functions in c?

551


What are the application of void data type in c?

695


Is anything faster than c?

580


Badboy is defined who has ALL the following properties: Does not have a girlfriend and is not married. He is not more than 23 years old. The middle name should be "Singh" The last name should have more than 4 characters. The character 'a' should appear in the last name at least two times. The name of one of his brothers should be "Ram" Write a method: boolean isBadBoy(boolean hasGirlFriend , boolean isMarried, int age , String middleName , String lastName , String[] brotherName); isHaveGirlFriend is true if the person has a girlfriend isMarried is true if the person is married age is the age of the person middleName is the middle name of the person lastName is the last name of the person brotherName is the array of the names of his brothers

1420


Can the “if” function be used in comparing strings?

586


Explain what is page thrashing?

605


Read the following data in two different files File A: aaaaaaaadddddddd bbbbbbbbeeeeeeee ccccccccffffffff File B: 11111111 22222222 33333333 By using the above files print the following output or write it in the Other file as follows aaaaaaaa11111111dddddddd bbbbbbbb22222222eeeeeeee cccccccc33333333ffffffffffff

2238


Are the variables argc and argv are always local to main?

569


Write a C program in Fibonacci series.

629


Write a program in c to replace any vowel in a string with z?

684