0

I'm pretty new in programming and I making a decimal to binary converter. I need help to make the print output starts from right to left (reversed).(Sorry if my code is messy)

int main()
{
    int num, form;

    printf("Decimal to Binary\n\n");
    printf("        Value          : ");
    scanf("%d", &num);
    printf("        Expected Format (Type 2 for binary): ");
    scanf("%d", &form);
    if (form == 2)
        printf("        %d base 10 is ", num);
    if (form == 2)
        do {
            if (num % 2 == 0) {
                printf("0");
                num = num / 2;
            }
            else {
                printf("1");
                num = num / 2;
            }
        } while (num > 0);
    else
        printf("Invalid input!");

    return 0;
}

If I input the value to 25,I expected the output will be "11001", but the actual output is "10011"

mch
  • 9,424
  • 2
  • 28
  • 42
Deificz
  • 3
  • 1

2 Answers2

1

Some recursion.. It's looks much more better, for my opinion

#include <stdio.h>

void rec(int num)
{
    if (num==0) return;
    rec(num>>1);
    printf("%d", num%2);
}

int main()
{
  int n;

  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);
  printf("%d in binary number system is: ", n);

  rec(n);

  printf("\n");
  return 0;
}
brfh
  • 334
  • 1
  • 12
  • How is it better ? Recursion can cause many issues and here I really don't see the benefit. All you have to do is shift the integer or char bit per bit and print the value of the least significant bit at each iteration, as @Tscheppe proposed – Guillaume Petitjean Sep 05 '19 at 08:33
  • I just wrote this code it to show how will be look recursion in this case. In fact it's really slower than realization with loop, but a bit compactor – brfh Sep 05 '19 at 08:42
  • 1
    The benefit of the recursion here is that the result may already be printed in the correct order, the code is small and easy to maintain. Additionally I don't see where recursion could cause an issue in this particular situation, too. There are always many ways to achieve a goal and you should just choose the one you are most familiar with. – Trickzter Sep 05 '19 at 11:04
0

One possibility would be recursion as Jean-Francois Fabre said. Since you mentioned you are a beginner, recursions are sometimes hard to understand at the beginning, so another possibility that I didn't find in his link would be something like this

#include <stdio.h>

int main()
{
  int n, c, k;

  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);

  printf("%d in binary number system is:\n", n);

  for (c = 8; c >= 0; c--)
  {
    k = n >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }

  printf("\n");

  return 0;
}

You can use c to specify the length of your output, or calculate in advance to have a perfect output.

tscheppe
  • 588
  • 5
  • 18
  • Is there anyway to do add this "recursion" in my code without changing most of it or no? Thanks in Advance – Deificz Sep 05 '19 at 10:41