Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Decimal to Binary in C, result printed all mismatched
Forum Updated to NodeBB v4.3 + New Features

Decimal to Binary in C, result printed all mismatched

Scheduled Pinned Locked Moved Solved C++ Gurus
2 Posts 2 Posters 395 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Sachin BhattS Offline
    Sachin BhattS Offline
    Sachin Bhatt
    wrote on last edited by Sachin Bhatt
    #1

    I made a code in c that switches decimal to binary and vice versa, the issue I have is that when I do the convertion from decimal to binary it prints the numbers all mixed up, model, assuming I input the number 123 as a decimal it ought to print 11110110 however my program changes over it as 10111110

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

    Here is the article I was reading before trying this all stuff https://www.scaler.com/topics/decimal-to-binary-in-c/

    Thanks in advance!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Your not doing the operations in the correct order and as the scaler various demonstration does, you are printing your result in the wrong order.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved