There are a lot of problems here...
while(i<0){
So it will only enter the loop if the user enters a negative number...
Fix that, and it'll never end:
while(i>0){
i=a;
Because you reset the loop variable each time round the loop...
Fix that, and the bit at the bottom gets you:
c=i%2;
switch(c){
case '0':
i=i/2;
case '1':
i=(i-1)/2;
}
For two reasons:
1) c will never be equal to a character '0' or '1' - because it's a remainder when divided by 2, so it's a number 0 or 1 and they are different.
Fix that:
c=i%2;
switch(c){
case 0:
i=i/2;
case 1:
i=(i-1)/2;
}
And it still doesn't work right - though it's a bit better - because it drops through from
case 0
to
case 1
.
Fix that:
c=i%2;
switch(c){
case 0:
i=i/2;
break;
case 1:
i=(i-1)/2;
}
And it's almost there - not quite, but getting there - close enough that you should be able to fix the remaining problems.
You could have worked all this out for yourself, just by following exactly what the code was doing - and you will probably need to do that to fix the last problem.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Quote:
I tried this but i want to reverse the number and also numbers divisible by 2 are giving a problem.
Yes they will - because you don't know how to "end things"
And reversing the number is exactly what you want to do. That means finding the most significant binary digit and printing from there, or alway printing the same number of binary bits.
I don't know what system you are working on, so I don't know how "big" an integer is: it could be 16 bits:
−32,768 through 32,767
Or 32 bits:
−2,147,483,648 through 2,147,483,647
But if we assume the lower range and ignore negatives then it's pretty simple.
What you need is a shifting mask: first it isolates one bit in the highest position, then you shift it right so next time it looks one bit lower.
So, try this:
int x = 32766;
int mask = 0x4000;
int print = 0;
while (mask != 0)
{
if (print || (x & mask))
{
printf("%d", (x & mask) ? 1 : 0);
print = 1;
}
mask >>= 1;
}
printf("\n");
Change the initial setting of
print
and it will "fix" the number of bits to "all 15".
How does it work?
Well ... the initial value of
mask
is a single bit: bit14 (numbering starts at 0 for the least significant here): in binary that is:
100000000000000
A one followed by fourteen zeros.
Setting
print
to 0 makes it "false" as far as C is concerned - it has no inbuilt idea of "true" or "false" so any non-zero value is true, zero is false.
We loop while the
mask
still has bits to check.
Inside the loop we check: if
print
is non-zero we always print the digit, otherwise we check
x
with the
mask
. The
&
operator in C does a
BINARY AND
: it returns a value where only those bits that are 1 in both operands, all others are zero. Since
mask
only contains a single one bit, it isolates the value of that bit in x. (If you don't understand, Wikipedia can help you with pictures)
If we get a "go" from either, we print the binary digit and always set
print
to non-zero to ensure all subsequent digits are printed regardless of being zero or one.
Then we shift the
mask
one place right: 0x4000 becomes 0x2000, 0x1000, 0x0800, ... ready for the next digit.
When we have printed bit 0,
mask
will be one, so the shift will make it zero, and that will stop the loop.
Make sense?