Decimal to Binary

    1

    0

    Giovanny Gongora

    Codiga's C++ Recipes

    Convert decimal to binary number

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    long long convert(int);
    long long convert(int n) {
      long long bin = 0;
      int rem, i = 1;
      while (n!=0) {
        rem = n % 2;
        n /= 2;
        bin += rem * i;
        i *= 10;
      }
      return bin;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.