Skip to content

Instantly share code, notes, and snippets.

@amirfefer
Last active December 27, 2016 00:16
Show Gist options
  • Save amirfefer/529033f4619b627ac6045716215b5a95 to your computer and use it in GitHub Desktop.
Save amirfefer/529033f4619b627ac6045716215b5a95 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
using namespace std;
int convertibaseto10(int Num, int ibase)
{
int tmp1 = Num;
int decimal = 0;
int ex = 0;
while ( tmp1 >= 1)
{
decimal += (tmp1 % 10)*pow(ibase, ex);
ex++;
tmp1 = tmp1 / 10;
}
return decimal;
}
void convert10tofbase(int tmp1, int fbase)
{
int arr[10] = { 0 };
int i = 0;
while (tmp1 >= 1)
{
arr[i] = tmp1 % fbase;
i++;
tmp1 = tmp1 / fbase;
}
for (i = 9; i >= 0; i--)
if (arr[i] > -1 )
cout << arr[i];
}
int convertNum(int input_num,int ibase, int fbase)
{
if (ibase < 2 || ibase>10)
{
cout << "Problem with input!" << endl;
return 0;
}
int tmp = input_num;
while (tmp >= 1)
{
if (tmp % 10 >= ibase)
{
cout << "Problem with input!" << endl;
return 0;
}
else
tmp = tmp / 10;
}
int newnum = convertibaseto10(input_num, ibase);
convert10tofbase(newnum,fbase);
}
int main()
{
int input_num, ibase, fbase;
cout << "Enter number:";
cin >> input_num;
cout << "Enter input base:";
cin >> ibase;
cout << "Enter output base:";
cin >> fbase;
convertNum(input_num,ibase,fbase);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment