I have to create a program that converts a decimal number into binary, hexadecimal, and binary-coded decimal.
I have hexadecimal to work, however, both binary codes are what I am lacking.
I have a good understanding of how both binary and binary-coded decimal works, I just can't translate that to a c++ program.
Can anyone help me with it? I know there is a code of decimal to binary conversion, however, it has a recursion and I can't use that because we haven't learned recursion before.
If anyone can turn that code to a working code without recursion, that would be great.
Here is the code (of the recursion):
#include <iostream.h>
void binary(int);
void main(void) {
int number;
cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else {
cout << number << " converted to binary is: ";
binary(number);
cout << endl;
}
}
void binary(int number) {
int remainder;
if(number <= 1) {
cout << number;
return;
}
remainder = number%2;
binary(number >> 1);
cout << remainder;
}