如何在不使用乘法的情况下使用while进行乘法? [英] How do you multiply using while without using multiplication?

查看:132
本文介绍了如何在不使用乘法的情况下使用while进行乘法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何仅使用加法或减法运算符而不使用除法和乘法来获得两个整数的乘积.如果您可以添加while语句,将很有帮助. A

I want to know how to get the product of two integers using only the addition or subtraction operators and without using the division and multiplication. If you could add the while statement that would be helpful. A

基本上,我想知道如何根据用户定义的次数增加一定数量.将x本身加y次.要让用户定义循环执行此操作的次数,请使用int().谢谢,请在必要时使用注释.我对此还是有点陌生​​,谢谢.

Basically, I want to know how to add a certain number a certain number of times as defined by the user. Adding number x to itself y number of times. To have the user define the number of times to loop this, use int (). Thanks and please use comments where necessary. I am still a bit new to this and thank you.

这是我当前的代码:

# Asks user for two numbers to multiply
print ('Give me two numbers to multiply.')
print ()
# Gets input from the user
x = int ( input ('First Number: '))
y = int ( input ('Second Number: '))
z = 0
# Does the "multipling"
while z <= x*y:
    print (z)
    z = z + x
    time.sleep(.2)

++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

感谢您的帮助... 我想通了

Thanks for the help... i figured it out though

导入时间

print(两位数乘法计算器") 打印('=================================' 打印 () 打印(给我两个数字.")

print ('Two Digit Multiplication Calculator') print ('===================================') print () print ('Give me two numbers.')

x = int(输入(':'))

x = int ( input (':'))

y = int(输入(':'))

y = int ( input (':'))

z = 0

而x> 0: 列印(z) 打印 () x = x-1 z = y + z time.sleep(.2)

while x > 0: print (z) print () x = x - 1 z = y + z time.sleep (.2)

打印(z + x)

推荐答案

您可以重复使用加法.

def multiply(a,b):
    total = 0
    counter = 0
    while counter < b:
        total += a
        counter += 1
    return total

>>> multiply(5,3)
15

考虑一下,要将两个整数相乘,只需将一个整数相乘多次即可.例如:

Think about it, to multiply two integers, you just add one integer that many times. For example:

5 x 3 = 5 + 5 + 5 = 15

这篇关于如何在不使用乘法的情况下使用while进行乘法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆