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);
}
}

in decimal to binary, calcuation is ok. you are priinting binary number in reverse order

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

Using String would be better.

yeah

how to
edit the post?


any solutions

How can i edit my post ?

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++);
}

is the code ok now ?

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'm using Turbo C.

Oops

Having apower cut - got to shut down

That's what you get for using Turbo C ;)

Better option (IMO) are:
- visual studio express
- Bloodshed DevC
- code::blocks

They will help you program the standard C way instead of something Turbo thinks is standard. They're all free to download

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.

Click here

Also: If you have a question, you should start your own thread, not hijack someone else's.

Erm Niek, its my thread actually

Click here

Also: If you have a question, you should start your own thread, not hijack someone else's.

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)

yup

thnx. reading about pointers seriously

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();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.