There has been a lot of confusion for Binary to decimal conversion and vice versa. Today morning i came up with some simple self understandable code !!!

Binary to Decimal

#include <stdio.h>
#include <math.h>
#include <graphics.h>

main()
{
int dec;
int total;
int power;
clrscr();

total=0;
power=1;
printf("Enter binary number :\n");
scanf("%d",&dec);
while(dec>0)
{
total += dec%10*power;
dec=dec/10;
power=power*2;
}
printf("%d is the decimal number",total);

}

Decimal to binary

#include <stdio.h>
#include <graphics.h>

main()
{
int decimal=0,rem=0;
clrscr();
printf("Enter Decimal : ");
scanf("%d",&decimal);
while(decimal>0)
{
rem=decimal%2;
decimal=decimal/2;
printf("%d",rem);
}
}

oops sorry. We need to take it into an array and reverse it or reverse the string

I am new to forums too, as far as I can see, You can edit you post right after you post a message.

I have edited the code and it is as follows :

There has been a lot of confusion for Binary to decimal conversion and vice versa. Today morning i came up with some simple self understandable code !!!

Binary to Decimal

#include <stdio.h>
#include <math.h>
#include <graphics.h>

main()
{
int dec;
int total;
int power;
clrscr();

total=0;
power=1;
printf("Enter binary number :\n");
scanf("%d",&dec);
while(dec>0)
{
total += dec%10*power;
dec=dec/10;
power=power*2;
}
printf("%d is the decimal number",total);

}

Decimal to binary

#include <stdio.h>
#include <graphics.h>

main()
{
int decimal=0,rem=0;
char *ptr1,*ptr2,bin;
clrscr();
printf("Enter Decimal : ");
scanf("%d",&decimal);
while(decimal>0)
{
rem=decimal%2;
decimal=decimal/2;
printf("%d",rem);
scanf("%d",&bin);
}
*ptr1=*ptr2 = bin;
while (*ptr2)
putchar(*ptr2++);
}

Close, but a few things:
- it's int main() and return 0; Click here for info
- Clrscr() is not standard. I'm using VS2008 and my compiler would say "identifier not found". Click here for info
- same goes for #include <graphics.h> Did you test the Decimal to binary? I don't have a compiler on this PC, but I would be suprised if it worked....
What's scanf("%d",&bin); doing there?

I agree with that, printed to stdout & scaning from stdin. your code is wrong & I guess, you have not yet learned pointers. So, I would use arrays hear

declare char c[30] ;
in the loop, put c[i++] = rem ;
then in another while loop
while ( i ) putchar ( c ;

I have VS 2008 Professional. I am learning pointers. Does anyone know a good pointer lesson ??

I learn from The C PRimer Second Edition

I think pointers are too much for my age - 12.

Erm Niek, its my thread actually

So I see...

Perhaps my brain misfired there for a second. Sorry bout that. But the link is still usefull (about pointers)

Sorry. I have been away for ages.

This is the code i have found error free and spent some more time rebuilding it. Simple as a pie

#include<stdio.h>
#include<graphics.h>

main()
{
int dec,n,i,j,k;
int ma[30];
clrscr();
printf("Enter decimal number : \n");
scanf("%d",&dec);
i=0;
while(dec>0)
{
ma[i]=dec%2;
dec = dec/2;
i++;
}
k=i-1;
for(j=0;j<i;j++)
{
printf("%d",ma[k]);
k--;
}
printf(" is the number in binary form");
getch();
}
This question has already been answered. Start a new discussion instead.