How To Write C++ Program To Convert Decimal Number Into Binary Number

file1[1]

This is a simple C++ code that converts any decimal number into binary number system. The program takes input from user in decimal number form and return output in binary number system using a recursive function that takes decimal number as a parameter for it.

The C++ program is successfully compiled and run on a Windows OS system using Microsoft’s Visual Studio 2013.

EXAMPLE OUTPUT:

Enter any decimal number: 2

After converting 2 in binary form 0010

CODE:

//Program to Convert Decimal number into Binary number

#include<iostream>
using namespace std;

void binary(int num)
{
int rem;

if (num <= 1)
{
cout << num;
return;
}
rem = num % 2;
binary(num / 2);
cout << rem;
}

int main()
{
int dec, bin;
cout << “Enter any decimal number : “;
cin >> dec;

if (dec < 0)
cout << dec << ” Seems not a positive number. Try another one next time.” << endl;
else
{
cout << “After converting ” << dec << ” into binary form: “;
binary(dec);
cout << endl;
}
return 0;
}

Write in below comments if you have any tutorial request or have any assignment pending from your college. How Strings will get back to you for your help as soon as possible. Follow How Strings for more tech tutorials!

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *