3.python program to read two numbers and print their quotient and remainder

Latest from djangocentral

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…

©

djangocentral | Djangocentral is not associated with the DSF | Django is a registered trademark of the Django Software Foundation

Article Creation Date : 22-Jun-2021 10:59:15 PM


Description:

To get the quotient on dividing two numbers in Python, we use the // (integer division) operator.

To get the remainder on dividing two numbers in Python, we use the % (modulus) operator.

Python Code:

class Division: def __init__(self, num1, num2): self.num1 = num1 self.num2 = num2 def get_quotient(self): return self.num1 // self.num2 def get_remainder(self): return self.num1 % self.num2 def main(): num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) obj = Division(num1, num2) quotient = obj.get_quotient() remainder = obj.get_remainder() print("\nQuotient:", quotient) print("Remainder:", remainder) if __name__ == "__main__": main()

Output:

Enter first number: 20 Enter second number: 3

Quotient: 6 Remainder: 2


Views : 1473

ABOUT THE AUTHOR

3.python program to read two numbers and print their quotient and remainder

Siddhartha Dutta

India


In this example, we will write a simple program that takes input from the user and calculates the quotient and remainder operation in Python. The quotient is the result of the division operation while the remainder is the value remaining after the division operation. To better understand this example, make sure you have knowledge of the following tutorials:-

  • Python Operators
  • Python Input and Output
  • Python Data Type Conversion

Source Code:

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)

The output of the above program is:-

Enter the first number: 16 Enter the second number: 3 Quotient is: 5

Remainder is: 1

In this program, the user input is taken using input() function which takes input as a string. But as we are doing the calculation part, we need the input to be an integer. So, we did the type-conversion using int() function.

The input from the user is taken as a and b and the operator // is used for calculating quotient and % for calculating remainder. The result is displayed using print () function.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Given two numbers , the task is to print their quotient and Remainder in  Python.

Examples:

i)Floating Division

Example1:

Input:

first number =45 second number =17

Output:

The value of quotient after dividing 45 / 17 = 2.6470588235294117 The value of remainder after dividing 45 / 17 = 11

ii)Integer Division

Input:

first number =45 second number =17

Output:

The value of quotient after dividing 45 / 17 = 2 The value of remainder after dividing 45 / 17 = 11

Program to Read Two Numbers and Print Their Quotient and Remainder in  Python

Below are the ways to print the quotient and Remainder:

1)Using / and % modulus operator in Python (User Input separated by new line, Float Division)

Approach:

  • Scan the given two numbers using int(input()) and store them in two separate variables.
  • Calculate the quotient by using the syntax first number /second number and store it in a variable.
  • Calculate the remainder by using the syntax first number %second number and store it in a variable.
  • Print the above two variables which are the result of the program
  • Exit of Program.

Below is the implementation:

# scanning the given two numbers using int(input()) function # first number numb1 = int(input("Enter some random number = ")) # second number numb2 = int(input("Enter some random number = ")) # Calculate the quotient by using the syntax first number /second number # and store it in a variable. quotie = numb1/numb2 # Calculate the remainder by using the syntax first number %second number # and store it in a variable. remain = numb1 % numb2 # Print the above two variables which are the result of the program print("The value of quotient after dividing", numb1, "/", numb2, " = ", quotie) print("The value of remainder after dividing", numb1, "/", numb2, " = ", remain)

Output:

Enter some random number = 86 Enter some random number = 12 The value of quotient after dividing 86 / 12 = 7.166666666666667 The value of remainder after dividing 86 / 12 = 2

2)Using / and % modulus operator in Python (User Input separated by spaces , Float Division)

Approach:

  • Scan the given two numbers using map and split() functions to store them in two separate variables.
  • Calculate the quotient by using the syntax first number /second number and store it in a variable.
  • Calculate the remainder by using the syntax first number %second number and store it in a variable.
  • Print the above two variables which are the result of the program
  • Exit of Program.

Below is the implementation:

# Scan the given two numbers using map and split() functions # to store them in two separate variables. numb1, numb2 = map(int, input("Enter two random numbers separated by spaces = ").split()) # Calculate the quotient by using the syntax first number /second number # and store it in a variable. quotie = numb1/numb2 # Calculate the remainder by using the syntax first number %second number # and store it in a variable. remain = numb1 % numb2 # Print the above two variables which are the result of the program print("The value of quotient after dividing", numb1, "/", numb2, " = ", quotie) print("The value of remainder after dividing", numb1, "/", numb2, " = ", remain)

Output:

Enter two random numbers separated by spaces = 45 17 The value of quotient after dividing 45 / 17 = 2.6470588235294117 The value of remainder after dividing 45 / 17 = 11

3)Using // and % modulus operator in Python (User Input separated by spaces , Integer Division)

Approach:

  • Scan the given two numbers using map and split() functions to store them in two separate variables.
  • Calculate the integer quotient by using the syntax first number //second number and store it in a variable.
  • Calculate the remainder by using the syntax first number %second number and store it in a variable.
  • Print the above two variables which are the result of the program
  • Exit of Program.

Below is the implementation:

# Scan the given two numbers using map and split() functions # to store them in two separate variables. numb1, numb2 = map(int, input("Enter two random numbers separated by spaces = ").split()) # Calculate the quotient by using the syntax first number /second number # and store it in a variable. quotie = numb1//numb2 # Calculate the remainder by using the syntax first number %second number # and store it in a variable. remain = numb1 % numb2 # Print the above two variables which are the result of the program print("The value of quotient after dividing", numb1, "/", numb2, " = ", quotie) print("The value of remainder after dividing", numb1, "/", numb2, " = ", remain)

Output:

Enter two random numbers separated by spaces = 45 17 The value of quotient after dividing 45 / 17 = 2 The value of remainder after dividing 45 / 17 = 11

Related Programs: