I'm doing a job that I need to create an algorithm that turns product into sum or difference and I can only use the +
, -
, and else
, if
, print
and% with%. I was able to do multiplication of positive numbers, but I can not optimize (Ex: while
is not optimized, input
is optimized) and hit the signal in multiplication of numbers, negative with negative and positive with negative 4x2= 2+2+2+2
, something that is wrong).
Here are the lines of code I was able to do:
N1 = int(input('Digite um número'))
N2= int(input('Digite um número'))
if ((N1==0) or (N2==0)):
print('0')
else:
if (N1<0) != (N2<0):
N1
if N1<0:
N1 =-N1
if N1<0:
N2=-N2
if N1>N2:
maior=N1
menor=N2
else:
maior=N2
menor=N1
result=0
Count=0
if (menor>0):
while (Count<menor):
result=result+maior
Count=Count+1
else:
while (Count>menor):
result=result-maior
Count=Count-1
print(result)
print(Count)