Python 程序查找两个数的商和余数


2022年4月29日, Learn eTutorial
1941

在这个简单的 Python 程序中,我们需要找到商和余数。这是一个初级的 Python 程序。

要理解这个例子,您应该了解以下 Python 编程主题

如何在 Python 中找到商和余数?

这个简单的 Python 程序是关于 Python 中算术运算的除法。这里我们需要将一个数除以给定的数,并打印商和余数。例如,考虑两个数 125 和 7,我们将 125 除以 7,得到商为 17,余数为 6。

为了在 Python 语言中应用除法逻辑,我们接受用户输入的两个数,然后分别计算商和余数。为了找到商,我们将一个数除以给定的数,并将结果保存到变量 quotient 中。其次,我们使用 **取模** 运算符计算余数,并将该值保存为 remainder。最后,使用 Python 的基本方法和语法打印商和余数。

算法

步骤 1: 使用 input 方法接受用户输入的两个除数,并使用 Python 编程语言中的 int() 将字符串转换为整数。

步骤 2: 在一个数除以给定数之后,保存商的值。

步骤 3: 使用 **取模** 运算符保存余数的值。

步骤 4: 使用 Python 中的 print 打印商和余数的值。

Python 源代码

                                          a=int(input("Enter the first number: "))

b=int(input("Enter the second number: "))

quotient=a//b

remainder=a%b

print("Quotient is:",quotient)

print("Remainder is:",remainder)
                                      

输出

Enter the First number : 125

Enter the Second number : 7

Quotient is :  17

Reminder is : 6