I'm new to C and I'm trying to convert decimal to binary. The result is giving me an inverse number of what's required. I tried to apply modulo on the result like I saw on other forums but I still get the same result. Any help?

#include<stdio.h>

int main()
{
    int number;
    long int quotient, rem, result=0;
    printf("Enter a number: ");
    scanf("%d", &number);

    quotient=number;

    while (quotient!=0)
    {
        quotient=quotient/2;
        rem=quotient%2;
        rem = rem%10;
        printf("%ld", rem);
    }
}

Here's one way to do it.

Because you're essentially looking at the least significant bit first, you're printing them first, and as a result, the number is being printed backwards.

A couple of notes here. The line rem = rem%10 more than likely does not do anything because the line rem=quotient%2 guarantees that rem will either be 1 or 0. The line quotient=quotient/2 may cause you to lose the first bit of your answer completely as well.

I'm not sure what the other answers looked like, but division and modulo may not be the best way to print this stuff out, especially in C where prepending to a string is not trivial. It may be a good idea to look into actual bitwise math operations.

I'm not sure what the other answers looked like, but division and modulo may not be the best way to print this stuff out, especially in C where prepending to a string is not trivial. It may be a good idea to look into actual bitwise math operations.

It's decent enough, provided you reverse the string before printing it. This of course brings into question whether you can afford the memory cost of storing the characters, but often that cost is negligible.

Another alternative, assuming the number of bits are relatively small, is recursion.

i tried many times please let me know what's wrong with my code

#include<stdio.h>

void Reverse(long int n)
{
    long int reverse = 0, rem;
    while (n!=0)
    {
        rem=n%10;
        reverse=reverse*10+rem;
        n/=10;
    }
    printf("%ld", reverse);
}

int main()
{
    int number;
    long int quotient, rem;
    printf("Enter a number: ");
    scanf("%d", &number);

    quotient=number;

    while (quotient!=0)
    {
        quotient=quotient/2;
        rem = quotient%2;
        Reverse(rem);
    }

}

You're calling your Reverse() function over and over again, in a loop. This function doesn't have enough context to know exaclty what's going on and how to print things in the correct order.

What needs to be done, is you may have to generate the entire string, and then print it backwards after all the number crunching has been done.

i'm not allowed to use arrays or strings :(

You can do the whole thing as a recursive function without a while loop.

The sudo code would look something like

PrintBinary(Number)
{
    IF Number is 0 Return   // End Case

    Get Least Significant Bit
    Number = Number With Least Significant Bit Removed
    PrintBinary(Number)

    Print Least Significant Bit
}

Because you recurse at line 7 before printing the bit at line 9 you end up actually printing the number most significant bit first even though you are extracting the least significant bits.

All the data that you might normally store in an array or string is held on the stack for you by the function recursion

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.