Number in base ten to any base

Given as input a number in base ten, convert it to any other base.
Example:
INPUT
Number = 14
Base = 2
OUTPUT
1110

This is the code I wrote:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main () {
	//Initializing:
	int number = 0, base = 0;
	int digit = 0, result = 0;
	
	//Input:
	cout<<"Number = ";
	cin>>number;						//Giving the input.
	cout<<"Base = ";
	cin>>base;						//Giving the input.
	
	//Procedure:
	do {
		digit = number%base;			//Taking the single digit of the number.
		result = number/base;			//Doing the division and
		number = result;			//initialising "number" for the next loop.
		cout<<digit;
	} while (result >= 1);
	
	//Terminating program:
	return 0;
}


How do I swap the digits? Because this way it writes the digits backwards. I don't have any idea how to do that.
Last edited on
how about creating an array and put each digit into the array, and then cout the array as you like after the do while loop is done?
Yeah, I just realized that I needed an array. I did it! Thanks!
Topic archived. No new replies allowed.