Arithmetic Caculation

Python program to Divide two numbers|in 4ways

Python program to Divide two numbers

In this tutorial, we will discuss the Python program to Divide two integers|in 5ways

In this post, we are going to learn how to find division of two numbers using different 5 ways in Python language

Program to Divide two numbers

Program to Divide two numbers-standard method

Program 1

num1=250
num2=25

div=num1/num2;

print("The division of {0} and {1} is: {2}".format(num1,num2,div))

When the above code is executed, it produces the following result

The division of 250 and 25 is: 10

 

  • Integer variable num1 and num2 both are declared and initialized
  • the num1 and num2 both are divided together and the value assigned to the integer variable div
  • finally the program is displayed the output using print() function

 

Program to Divide two numbers-using user input

Program 2

num1=input("Enter the first number: ")
num2=input("Enter the second number: ")

div=num1/num2;

print("The division of {0} and {1} is: {2}".format(num1,num2,div))

When the above code is executed, it produces the following result

Enter the first number: 2400
Enter the second number: 80
The division of 2400 and 80 is: 30

 

Approach

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1 and num2
  • the program will read the input and store the variables in num1, num2.
  • the num1 and num2 both are divided together and the value assigned to the integer variable div
  • finally the program is displayed the output using print()

 

Program to Divide two numbers-using function

Program 3

#python program to divide two numbers using function
def divNum(a,b):#function definision for division
    divide=a/b
    return divide
num1=int(input("input the number 1 : "))#input from user to num1
num2=int(input("input the number 2 : ")) #input from user to num2

print("The division is",divNum(num1,num2))#call the function

When the above code is executed, it produces the following result

input the number 1 : 450
input the number 2 : 30
The division is', 15

 

Approach

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input  store the variables in num1, num2.
  • define the function to find division of two numbers
  • When we call the function, the num1 and num2 both are divided together
  • Then the value is assigned to the integer variable result
  • finally the program is displayed the output using print() function

Program to Divide two numbers-using recursion

Program 4

#python program to divide two numbers using function
def divNum(x,y):#function definision for division
    if (y==0):
        return 0;
    elif (x-y==0):
        return 1;
    elif (x<y):
        return 0;

    else:
        return (1+divNum(x-y,y));

num1=int(input("input the number 1 : "))#input from user to num1
num2=int(input("input the number 2 : ")) #input from user to num2

print("The division is",divNum(num1,num2))#call the function

When the above code is executed, it produces the following result

input the number 1 : 1000
input the number 2 : 40
('The division is', 25)

 

Approach

  • Integer variable x and y both are declared,
  • The program takes input from the user
  • Then the user enters the input value for x and y
  • the program will read the input  store the variables in  x , y.
  • define the recursive function for multiply two numbers
  • When we call the function, the x and y both are multiplied recursively
  • finally the program is displayed the output using print() function

 

Suggested post

Function in Python language

Operator in Python language

Data type in Python

 

 

Similar post

C program to calculate division of two numbers | 6 different Methods

C++ program to calculate division of two numbers | 6 different Methods

Java program to calculate division of two numbers | 5 different Methods

 

 

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

10 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

10 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

10 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

10 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.