Write a program to Convert a Decimal number to a Binary number

#include<iostream>
#include<cmath>
using namespace std;

int decimaltobinary(int n){
   // Divison Method
   int binaryno = 0;
   int i = 0;
   while(n>0){
      int bit = n % 2;
      binaryno += bit*pow(10,i);
      // cout << binaryno << endl;
      n = n/2;
      i++;
   } 
   return binaryno;
}

int main(){
   int n;
   cout << "Enter the number for its binary representation: ";
   cin >> n;
   int binary = decimaltobinary(n);
   cout << binary << endl;
   }
Scroll to Top
logo
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.