Need help with simple conversion code

Hello,

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;
}

Source: http://groups.engin.umd.umich.edu/CIS/course.des/cis400/cpp/binary.html#notes

Once I get this to work for binary, I should easily be able to do the binary-coded decimal on my own.
Last edited on
Surely you can figure out a way to turn the recursive call into a loop!
Topic archived. No new replies allowed.