TheAlgorithms-C-Plus-Plus/others/decimal_to_binary.cpp

26 lines
413 B
C++
Raw Normal View History

2019-08-21 10:10:08 +08:00
// This function convert decimal to binary number
2018-08-02 17:02:37 +08:00
//
#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
2019-08-21 10:10:08 +08:00
do
{
remainder = number % 2;
number = number / 2;
binary = binary + (remainder * var);
2018-08-02 17:02:37 +08:00
var = var * 10;
2016-08-09 18:20:07 +08:00
2019-08-21 10:10:08 +08:00
} while (number > 0);
2018-08-02 17:02:37 +08:00
cout << "the binary is :";
cout << binary;
cout << endl;
2019-02-09 15:24:49 +08:00
return 0;
2016-08-09 18:20:07 +08:00
}