-2

I made a code in c that converts decimal to binary and viceversa, the problem i have is that when i do the convertion from decimal to binary it prints the numbers in the wrong order, for example, if i input the number 123 as a decimal it should print 11110110 but my program converts it as 10111110

here is the code:

#include <stdio.h>
#include <stdlib.h>
main()
{
    int i, no, coc, re, opt;
    int dec;
    int bin, p, c;
    printf ("\n1. Decimal to binary \n2. binary to decimal \n choose an option: ");
    scanf("%d", &opt);
    switch(opt)
    {
        
        
        case 1:
        printf("\nDecimal No.: ");
        scanf("%d",&no);
        if (no>2)
        {
            printf("Your binary No.: ");
            for (i=1;1<=no; i++)
            {
                coc=no/2;
                no=coc;
                re=coc%2;
                printf("%d", re);
            }
        }
        break;
    
    
    
        case 2: printf("\nBinary No.: ");
        scanf("%d",&bin);
        c=1;
        dec=0;
        while(bin>0)
        {
            p=0;
            p=c*(bin%10);
            dec+=p;
            c*=2;
            bin/=10;
        }
            printf("Your decimal No.: %d",dec);
        break;
    
    
    
        default:
            printf("\a\n\nError");
        break;
    }   
    return 0;
}
LuisP
  • 1
  • 1
  • 1
    did you step through the code in a debugger, examine the variable values as they change? That is the 1st thing to do. – OldProgrammer Dec 11 '21 at 03:27
  • 1
    `if i input the number 123... it should print 11110110` - that would be 246, not 123. – selbie Dec 11 '21 at 03:27
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Dec 11 '21 at 03:28
  • Read your code carefully. You're not only printing the lower (least significant) bits first but you skip the least significant bit. – lurker Dec 11 '21 at 03:43
  • [Several Methods for Outputting Binary](https://stackoverflow.com/a/35477133/3422102) – David C. Rankin Dec 11 '21 at 06:36

1 Answers1

0

If you want your program to work correctly, consider storing the bits in an array and printing it from the last element. But that will be overkill for a simple program. Consider the following simple program. It just uses bitwise operations (generally, considered fast).

#include <stdio.h>
#include <stdlib.h>
static void DecToBin(n)
{
    int i=2;
    while(i<n)
    {
        i=i<<1;
    }
    for(;i>0;i=i/2)
    {
        printf("%d",i&n);
    }
}
main()
{
    int no, opt;
    int dec;
    int bin, c;
    printf ("\n1. Decimal to binary \n2. binary to decimal \n choose an option: ");
    scanf("%d", &opt);
    switch(opt)
    {
        case 1:
        printf("\nDecimal No.: ");
        scanf("%d",&no);
        if (no>0)
        {
            printf("Your binary No.: ");
            void DecToBin(n);
        }
        break;

        case 2: printf("\nBinary No.: ");
        scanf("%d",&bin);
        c=1;
        dec=0;
        while(bin>0)
        {
            dec+=c*(bin%10);
            c*=2;
            bin/=10;
        }
        printf("Your decimal No.: %d",dec);
        break;
    
        default:
            printf("\a\n\nError");
        break;
    }   
    return 0;
}

I have used the bitwise method. I hope it helps.

"<<" bitwise left shift
"&" bitwise and.

Kumar
  • 173
  • 1
  • 12