0% found this document useful (0 votes)
23 views4 pages

Base Conversion Algorithm Complexity

This program converts numbers between different number bases. It takes in a number as a digit string, the source base, and the target base. It uses two functions: mtodec converts the input number to decimal, and decton converts the decimal number to the output base number. mtodec iterates through the input string, calculating the place value of each digit and adding it to the decimal number. decton takes the remainder and quotient of the decimal number divided by the output base to extract each digit of the result.

Uploaded by

brainbarshan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Base Conversion Algorithm Complexity

This program converts numbers between different number bases. It takes in a number as a digit string, the source base, and the target base. It uses two functions: mtodec converts the input number to decimal, and decton converts the decimal number to the output base number. mtodec iterates through the input string, calculating the place value of each digit and adding it to the decimal number. decton takes the remainder and quotient of the decimal number divided by the output base to extract each digit of the result.

Uploaded by

brainbarshan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

Program : m to n bas conversion

Purpose of the program :


Purpose of this program is to convert a given integer to its corresponding number in
another base.

Theory :
To convert one number in any base to the number in another base we convert the given
number to its decimal equivalent. Then we convert the decimal number to the number in
output base.

Algorithm :
procedure mton_base_convert()
{
step 1: Input number as a digit string, source base and target base
step 2: call mtodec() to convert the input digit string in base m to decimal number and
store it in dec.
step 3: call decton to convert the decimal number dec to output digit string in base n
}

Function mtodec(input_string, m)
{
[This function converts a digit string in base m to decimal number]
step 1: dec=0, p=1
step 2: for(i= n to 1 decrease I by 1)
{
/*find the numeric value of input_string[i]*/
if(input_string[i]>=‘A’ AND input_string[i]<=’F)
d= input_string[i] – ‘A’ +10
else
if(input_string[i]>=‘a’ AND input_string[i]<=’f’)
d= input_string[i] – ‘a +10
else
if(input_string[i]>=‘0’ AND input_string[i]<=’9’)
d= input_string[i] – ‘0’
dec= dec + p*d;
p= p*m;
}

step 3: return dec


}
Function decton(dec , n)
{
[ This function converts the decimal number dec to its equivalent digit string in base n]
step 1: i=0;
step 2: do step 3 to step 6 while dec!= 0
step 3: c=dec%n
step 4: if(c>=0 AND c<=9)
c=c+’0’
else
if(c>=10 AND c<=15)
c=c+’A’-10
step 5: output_string[i] = c
step 6: dec=dec/n
i=i+1
step 7: return output_string
}

C code :
/***********************************************************************
Program to convert a number of any base to the corresponding number of another base.
***********************************************
************************/#include <stdio.h>
#include <string.h>
long int mtodec ( char [100] , int ) ;
int decton ( long int , int , char [100] ) ;

int main()
{
char ip[100] , op[100] ;
long int dec ;
int ib , ob , n ;
printf( "\n Enter input number: " ) ;
scanf( "%s", ip ) ;
printf( "\n Enter input base: " ) ;
scanf( "%d", &ib ) ;
printf( "\n Enter output base: " ) ;
scanf( "%d", &ob ) ;
/* calculate equivalent decimal number from input number */
dec = mtodec( ip , ib ) ;
/* calculate equivalent output number from decimal number */
n = decton ( dec , ob ,op ) ;
printf( "\n Output number:\n" ) ;
printf( "%s\n", op ) ;
return 0;
}
/* This function converts number in any base m to equivalent decimal number */
long int mtodec ( char ip[100] , int ib )
{
long int p=1 , dec=0 ;
int i , d ;
for( i = strlen( ip ) - 1 ; i >= 0 ;i-- )
{
if( ip[i] >= 'A' && ip[i] <= 'F' )
d = ip[ i ] - 'A' + 10;
else
if( ip[i] >= 'a' && ip[i] <= 'f' )
d = ip[ i ] - 'a' + 10 ;
else
if( ip[ i ]>= '0' && ip[ i ] <= '9' )
d = ip[ i ] - '0' ;
dec = dec + p*d ;
p = p*ib ;
}
return ( dec ) ;
}

/* This function converts a decimal number to equivalent number in any base n */


int decton( long int dec , int ob , char op[100] )
{
int i ;
char c ;
i=0;
do
{
c = dec % ob ;
if( c >= 0 && c <= 9 )
c = c + '0' ;
else
if( c >= 10 && c <= 15 )
c = c + 'A' - 10;
op[ i ] = c ;
dec = dec / ob ;
i++ ;
} while( dec != 0) ;
op[ i ] = '\0' ;
strrev( op ) ;
return ( i );
}
Output :
Enter input number: 10101
Enter input base: 2
Enter output base: 10
Output number:
21

Discussion :
Time complexity of this program is O(n).where n is the length of input digit string.
Conversion to binary or lower base than decimal base gives numbers of long length.
So we use here strings to store the input and output numbers.
So here integer to string and string to integer conversion is implemented.

You might also like