0% found this document useful (0 votes)
4 views7 pages

Exercise 3 Shreyansh

The document outlines a program designed to convert numbers from any base to another base as specified by the user. It includes an algorithm that first converts the input number to decimal and then to the desired base, utilizing two functions: AnyToDecimal and DecimalToAny. The program successfully implements this logic and demonstrates its functionality through user input and output examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Exercise 3 Shreyansh

The document outlines a program designed to convert numbers from any base to another base as specified by the user. It includes an algorithm that first converts the input number to decimal and then to the desired base, utilizing two functions: AnyToDecimal and DecimalToAny. The program successfully implements this logic and demonstrates its functionality through user input and output examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Exercise – 3

Number Base Conversion from Any Base to


Any Base

Name: Shreyansh Tiwari


Registration
24BCE10964
Number:
Course: CSE1021
BL20242504004
Class Number:
93
Aim
To make a program to convert a number of any given base to
any other base given by the user

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])

#Function to Convert from Any Base Number System to


Decimal Number System
def AnyToDecimal(Number2,base2):
Number2=str(Number2)
pwr=0
Deci_Num=0
for i in Number2[::-1]:
if 47<ord(i)<58:
Deci_Num+=(base2**pwr)*int(i)
else:
Deci_Num+=(base2**pwr)*(ord(i)-55)
pwr+=1
return(Deci_Num)

print('Enter the Number to be converted')


num1=input('=> ')
num1=num1.upper()
print('Enter the Base of GIVEN NUMBER')
num2=int(input('=> '))
print('Enter the Base to which',str(num1),'is to be
converted')
num3=int(input('=> '))
print() #Used just for Spacing the Output Lines

temp=AnyToDecimal(num1,num2)
result=DecimalToAny(temp,num3)

print(num1,'is converted to base',num3,'.\


nConverted Value is =>',result)

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.

Submitted by : Shreyansh Tiwari (24BCE10964)

You might also like