Exercise 3 Shreyansh
Exercise 3 Shreyansh
Algorithm
Workflow is to first convert the given number to Decimal
Base and then convert it to the given base by user from
Decimal Base. Below is the algorithm to do so:
Define Functions to do the conversions.
Our first function AnyToDecimal(Number1,base1) will
convert any number of decimal bases to any given base.
Step 1)Input: Number1 (decimal base number), base1
(target base)
Step 2)Output: NewBaseNum (number in target base)
Step 3)Initialize an empty string NewBaseNum.
Step 4)While Number1 is greater than 0:
Calculate the remainder ‘rem’ of Number1
divided by base1.
If rem is less than 10, convert it to a string and
append to it NewBaseNum.
Else, convert it to a character (A, B, C, etc.) with
ASCII Character conversion (like for
rem=10,11,12 or 13 convert it to A, B, C or D
respectively) and append it to NewBaseNum.
Update Number1 by dividing it by base1.
Step 5)Reverse NewBaseNum to get the final result and
return it.
Our Second function DecimalToAny(Number2,base2) will
convert a number of any base to Decimal base number.
Step 1)Input: Number2 (number in any base), base2
(base of the input number).
Step 2)Output: Deci_Num (number in decimal base).
Step 3)Convert ‘Number2’ to a string.
Step 4)Initialize ‘pwr’ to 0 and ‘Deci_Num’ to 0.
Step 5)For each character ‘i’ in ‘Number2’ (processed
from right to left):
If ’i’ is a digit from 0-9, convert it to an integer
and multiply by ‘base2’ raised to the ‘pwr’, then
add to ‘Deci_Num’.
Else, convert ’i’ to its corresponding value of
ASCII Character and subtract 55 from it, and
multiply by ‘base2’ raised to the ‘pwr’, then add
to ‘Deci_Num’.
Increment ‘pwr’ by 1.
Step 6)At last, return the ‘Deci_Num’.
Now it’s time to use this both functions together.
Prompt user to enter the number to be converted (num1).
Convert ‘num1’ to uppercase.
Prompt user to enter the base of the given number
(num2).
Prompt user to enter the base to which ‘num1’ is to be
converted (num3).
Convert ‘num1’ from base ‘num2’ to decimal using
AnyToDecimal function.
Convert the result of above from decimal to base ‘num3’
using DecimalToAny function.
Print the overall converted value.
Start Flowchart
Def DecimalToAny(Number1,base1):
NewBaseNum
=’‘
FALSE
Number1> return(NewBaseNum[::-1])
0
TRU Def
AnyToDecimal(Number2,base2):
rem=Number1%bas
e1
Number2=str(Number2
rem<1 )
TRU 0 FALSE pwr=0
Deci_Num=0
NewBaseNum
NewBaseNum +=chr(ord('A')
+=str(rem) +(rem-10))
x>=(-(len(Number2)))
FALSE TRU
Number1//=base1
i=Number2[x]
return(Deci_Num)
num1=input('Enter 47<ord(i)<58
the Number to be FALSE TRU
converted')
Deci_Num+
Deci_Num+=(base2**pwr =(base2**p
num2=int(input(' )*(ord(i)-55) wr)*int(i)
Enter the Base
of GIVEN
NUMBER'))
pwr+= x-=1
num3=int(input(
1
'Enter the Base
print(num1,'is converted to
to
base',num3,'.\nConverted
which',str(num1 Value is =>',result)
),'is to be
converted'))
temp=AnyToDecimal(num1,num2)
result=DecimalToAny(temp,num3) Stop
Program
#Function to Convert from Decimal Number System to
Any Base Number System
def DecimalToAny(Number1,base1):
NewBaseNum=''
while Number1>0:
rem=Number1%base1
if rem<10:
NewBaseNum+=str(rem)
else:
NewBaseNum+=chr(ord('A')+(rem-10))
Number1//=base1
return(NewBaseNum[::-1])
temp=AnyToDecimal(num1,num2)
result=DecimalToAny(temp,num3)
Output
Output 1
Output 2
Output 3
Conclusions
The program is successfully working as intended. It uses
while and for loops for constructing the logic. Use of ASCII
Characters was an important aspect of this program. The
conversion of a number of any base to any base was thus
successfully performed in this exercise.