Write a Python Program to Multiply Two Numbers by Repeated Addition

Hi guys, in this article you will learn about a Python program to multiply two numbers by  repetitive addition.

For example: let's take two numbers 2 and 3,  if you try to find the product you have to add 2 times the number 3  that is 3 + 3 = 6

If we take the same example 2 and 3 you have to add  number 2 three times  2 + 2 + 2 = 6

let's try to solve this using a python function.

 

 

Write a Python Program to Multiply Two Numbers by Repeated Addition

 

 

 


Write a Python Program to Multiply Two Numbers by Repeated Addition

Define a function named as multiply and pass the two integer values as input to the function.  take another variable total  to store the product of two numbers.

Use the for loop to rate from 0 to  second number.  During the for loop iteration keep adding the first number to the total.

def multiply(first, second):
   total = 0
   for _ in range(second):
       total += first
   return total





In the main program, we ask the user  to enter integer variables and store them into two variables,  call the function that we have defined above by passing to input arguments that user has entered and store the result into a new variable result.

Using the print function call print the result variable i.e,  there is a multiplying two numbers using repetitive addition

a = int(input("Enter first number : "))
b = int(input("Enter second number : "))

result = multiply(a, b)
print("Product of two numbers by repeated addition is : ", result)









Python Program
=========================
def multiply(first, second):
   total = 0
   for _ in range(second):
       total += first
   return total


a = int(input("Enter first number : "))
b = int(input("Enter second number : "))

result = multiply(a, b)
print("Product of two numbers by repeated addition is : ", result)





Output
==============
Enter first number : 34
Enter second number : 90
Product of two numbers by repeated addition is :  3060




Output
============
Enter first number : 12
Enter second number : 7
Product of two numbers by repeated addition is :  84






Conclusion
=================
Execute the above Python  program by providing to random integer variables and note down the results

Comment below if you have any  queries related to the above Python program.

 

 

 


Post a Comment

0 Comments