[ create a new paste ] login | about

Link: http://codepad.org/9b1Rhbl1    [ raw code | output | fork | 2 comments ]

C++, pasted on Apr 30:
#include <iostream>
using namespace std;
char metachar[] = "0123456789abcdef";

void tobasen(int x, int n)
{
    if (x > 0)
    {
        tobasen(x / n, n);
        cout << metachar[x % n];
    }
}

int main()
{
    int x = 100;
    for (int i = 2; i <= 16; i++)
    {
        cout << i << "进制";
        tobasen(x, i);
        cout << endl;
    }
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2进制1100100
3进制10201
4进制1210
5进制400
6进制244
7进制202
8进制144
9进制121
10进制100
11进制91
12进制84
13进制79
14进制72
15进制6a
16进制64


Create a new paste based on this one


Comments:
posted by Lin18001997521 on Dec 19
#include <stdio.h>
#include <math.h>
void main() {
int q, m, k, p, s[50];
double n;
m = 0;
printf("\n 请输入十进制数n:");
scanf_s("%lf", &n);
printf("\n 请输入转换进制数p(2-16):");
scanf_s("%d", &p);
printf(" (%f)10=(", n);
while (n != 0) {
q = fmod(n, p);
n = floor(n / p);
m++;
s[m] = q;
}
for (k = m; k >= 1; k--) {
if (s[k]>9) {
printf("%c", s[k] + 55);
}
else {
printf("%d", s[k]);
}

}
printf(")%d\n", p);
}
reply
posted by Lin18001997521 on Dec 19
#include <stdio.h>
#include <math.h>
void main() {
int q, m, k, p, s[50];
double n;
m = 0;
printf("\n 请输入十进制数n:");
scanf_s("%lf", &n);
printf("\n 请输入转换进制数p(2-16):");
scanf_s("%d", &p);
printf(" (%f)10=(", n);
while (n != 0) {
q = fmod(n, p);
n = floor(n / p);
m++;
s[m] = q;
}
for (k = m; k >= 1; k--) {
if (s[k]>9) {
printf("%c", s[k] + 55);
}
else {
printf("%d", s[k]);
}

}
printf(")%d\n", p);
}
reply