Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python หาผลหารและเศษจากการหาร 2 จำนวน


คำนวณผลหาร (Quotient) และเศษ (Remainder) จากการหารจำนวนเต็ม 2 จำนวน ด้วย // และ %

ตัวอย่างโค้ด

a = int(input("First number: "))
b = int(input("Second number: "))

print("First:", a)
print("Second:", b)

quotient = a // b
remainder = a % b

print("Quotient:", quotient)
print("Remainder:", remainder)

ผลลัพธ์

First number: 44
Second number: 56
Quotient: 0
Remainder: 44

คำอธิบาย

  • // Floor Division: ผลหารปัดลง (integer division)

  • % Modulo: เศษจากการหาร

  • 44 // 56 = 0, 44 % 56 = 44

  • ความสัมพันธ์: a = b × (a//b) + (a%b)

  • ใน Python: a % b มีเครื่องหมายเดียวกับ b (non-negative ถ้า b>0)