Decimal to Binary

PHOTO EMBED

Mon Nov 11 2024 16:09:30 GMT+0000 (Coordinated Universal Time)

Saved by @khadizasultana #c++ #function #loop

// Author : Khadiza Sultana
#include<iostream>
using namespace std;

int decimalToBinary(int decNum){
    int ans = 0, pow = 1;
    while(decNum > 0){
        int rem = decNum % 2; // documenting the remainder for example for 3 % 2 = 1
        decNum /= 2; // determining the divisor 3 / 2 = 1
        ans += (rem * pow); // determining the position for the remainder as it goes from down to up
        pow *= 10; // updating the position of the digits
    }
    return ans;
}

int main(){
    int decNum = 50;  
    cout << decimalToBinary(decNum) << endl;
    return 0;
}
content_copyCOPY