Python 程式讀取兩個數字並列印其商和餘數
當需要讀取兩個數字並列印它們除法後的商和餘數時,可以使用‘//’和‘%’運算子。
以下對其進行演示 -
示例
first_num = int(input("Enter the first number..."))
second_num = int(input("Enter the second number..."))
print("The first number is ")
print(first_num)
print("The second number is ")
print(second_num)
quotient_val = first_num//second_num
remainder_val = first_num%second_num
print("The quotient is :")
print(quotient_val)
print("The remainder is :")
print(remainder_val)輸出
Enter the first number...44 Enter the second number...56 The first number is 44 The second number is 56 The quotient is : 0 The remainder is : 44
說明
第一個和第二個數字作為輸入從使用者獲取。
它們顯示在控制檯上。
使用‘//’運算子求商。
使用‘%’運算子求餘數。
運算輸出分別賦值給兩個變數。
這作為輸出顯示在控制檯上。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP