c++ - convert a number from base-10 to another base -
c++ - convert a number from base-10 to another base -
i tried next code convert number base-10 base. works if there no zero(0) in destination base. check 79 , 3 , prints 2221 correct. seek number 19 , 3, result 21 instead of 201 indicates something's wrong.
int x, y, = 0, i, t, j; cout << "enter 2 numbers" << endl; cin >> x >> y; // x number in base-10 , x, destination base of operations = x; while (x >= y) { t = 1; (i = 0; x > y; i++) { x /= y; } cout << x; (j = 0; j < i; j++) { t *= y; } = - (t*x); x = a; } cout << x<<endl;
using recursive function easier using while
loop trying accomplish.
here's working program.
#include <iostream> using namespace std; void printinbase(int x, int y) { if ( x < y ) { cout << x; return; } int rem = x%y; printinbase(x/y, y); cout << rem; } int main() { int x, y; cout << "enter 2 numbers" << endl; cin >> x >> y; // x number in base-10 , x, destination base of operations printinbase(x, y); cout << endl; homecoming 0; }
c++
Comments
Post a Comment