Quinary to pentadecimal
May 9, 2016 at 12:44pm UTC
I've made a program that converts quinary number to pentadecimal. It works fine with most numbers, but with some, like 1143 it returns correct anwser with 0 at the beggining (1143 should be B8, but program returns 0B8). Can you help me with finding mistake in my code? I'd be grateful.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
using namespace std;
int main () {
int b,p,o,i,l,z,x,y;
int a;
cin >>a;
if (a<5)
cout <<a;
b=a;
y=a;
i=0;
while (b>0){
b=b/10;
i++;
}
o=1;
p=0;
l=i;
while (i>=0){
p=a%10*o+p;
a=a/10;
o=o*5;
i--;
}
x=p;
int t[l-2];
for (z=0;z<=l-2;z++){
x=p%15;
p=p/15;
t[z]=x;
}
for (z=l-2;z>=0;z--){
if (t[z]==10)
cout <<"A" ;
else if (t[z]==11)
cout <<"B" ;
else if (t[z]==12)
cout <<"C" ;
else if (t[z]==13)
cout <<"D" ;
else if (t[z]==14)
cout <<"E" ;
else
cout <<t[z];
}
}
May 9, 2016 at 3:56pm UTC
You are unlikely to find anyone who wants to wade through that.
Remember that number base (or
radix ) is a polynomial to make life easier for humans.
All base conversions work like this:
1 2 3
string s = <source number>
int n = convert_from( base_a, s );
string r = convert_to( base_b, n );
Hence, you'll need two functions: one to convert from (a base-5) string to an integer, and one to convert from an integer to a (base-15) string.
Hint: you only need a total of three variables maximum per function.
Topic archived. No new replies allowed.