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

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.