J
jyck91
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20
int temp, m, n, i, r, base10, true;
char num[LENGTH], result[LENGTH];
// This function is to convert a number in base M
// into a number in base 10.
void baseM_to_base10(void)
{
base10 = 0;
for( i=0; i<20 && true = 1; i++) // get the number from base N
{ num = getchar(); // sub. the number into string
if (num == '\n')
{num = '\0'; // if the above statement is true , num will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num == '1')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}
// This function is to convert a number in base 10
// into a number in base N.
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp[30],i;
for (i=0;i<30 && number<base;i++)
{
temp = number % base;
number = number / base;
}
temp = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp<10)
printf("%u",temp);
if (temp>10 || temp<36)
printf("%uc",temp+51);
}
}
main()
{
// m - base M (input base)
// n - base N (output base)
// num - inputted number in base M
// result - converted number in base N
// Variable Declaration
// Prompt the user to enter data required.
printf("Base Conversion\n");
printf("---------------\n");
printf("Please enter an inputted number: ");
scanf("%s",num);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// Perform Base Conversion
// - Call function baseM_to_base10 to convert the number
accordingly
// - Call function base10_to_baseN to convert the number
accordingly
// Display the converted number
system("PAUSE");
}
// The End Of Main Program
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20
int temp, m, n, i, r, base10, true;
char num[LENGTH], result[LENGTH];
// This function is to convert a number in base M
// into a number in base 10.
void baseM_to_base10(void)
{
base10 = 0;
for( i=0; i<20 && true = 1; i++) // get the number from base N
{ num = getchar(); // sub. the number into string
if (num == '\n')
{num = '\0'; // if the above statement is true , num will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num == '1')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}
// This function is to convert a number in base 10
// into a number in base N.
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp[30],i;
for (i=0;i<30 && number<base;i++)
{
temp = number % base;
number = number / base;
}
temp = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp<10)
printf("%u",temp);
if (temp>10 || temp<36)
printf("%uc",temp+51);
}
}
main()
{
// m - base M (input base)
// n - base N (output base)
// num - inputted number in base M
// result - converted number in base N
// Variable Declaration
// Prompt the user to enter data required.
printf("Base Conversion\n");
printf("---------------\n");
printf("Please enter an inputted number: ");
scanf("%s",num);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// Perform Base Conversion
// - Call function baseM_to_base10 to convert the number
accordingly
// - Call function base10_to_baseN to convert the number
accordingly
// Display the converted number
system("PAUSE");
}
// The End Of Main Program