TheAlgorithms-C-Plus-Plus/Decimal To Binary.cpp

25 lines
421 B
C++
Raw Normal View History

2018-08-02 17:02:37 +08:00
// This function convert decimal to binary number
//
#include "stdafx.h"
#include <iostream>
2016-08-09 18:20:07 +08:00
using namespace std;
2018-08-02 17:02:37 +08:00
2016-08-09 18:20:07 +08:00
int main()
{
int number;
2018-08-02 17:02:37 +08:00
cout << "Enter a number:";
cin >> number;
int remainder, binary = 0, var = 1;
2016-08-09 18:20:07 +08:00
2018-08-02 17:02:37 +08:00
do {
remainder = number % 2;
number = number / 2;
binary = binary + (remainder*var);
var = var * 10;
2016-08-09 18:20:07 +08:00
2018-08-02 17:02:37 +08:00
} while (number>0);
cout << "the binary is :";
cout << binary;
cout << endl;
2016-08-09 18:20:07 +08:00
}