![aliami0306oyaj0n](/avatars/29753.jpg)
aliami0306oyaj0n
09.11.2020 •
Computers and Technology
Write a program with 2 separate functions which compute the GCD (Greatest Common Denominator) and the LCM (Lowest Common Multiple) of two input integers.
Solved
Show answers
More tips
- F Food and Cooking Discover the Health Benefits of Cedar Nuts...
- S Science and Technology Exploring Our Galaxy: How Many Planets are in the Milky Way?...
- S Science and Technology Colliders: How They Work and Why They Matter...
- A Animals and plants Unraveling the Mystery of Loch Ness: What Does the Loch Ness Monster Look Like?...
- L Leisure and Entertainment How Many Seasons are There in the TV Show Interns?...
- S Sport Playing Bowling: Rules and Advice for Novices...
- L Leisure and Entertainment The Best Film of 2010: A Look Back at the Academy Awards...
- S Sport How to Learn Swimming? Simple Tips for Beginners...
- C Computers and Internet What is Web 2.0 and How Does it Work?...
- C Computers and Internet War of Social Media: Which Platform is the Leader?...
Answers on questions: Computers and Technology
- B Biology Match the following appropriately. Column A 1. Temperature: Temperature 2. The amount of energy needed to raise a temperature by 1: The amount of energy needed to...
- H Health Describe a situation in which one aspect of health has an impact on the other two...
- E English Paragraph describing your family members...
- C Computers and Technology What makes an App different than others?...
- M Mathematics Need help here ik this stuff is easy but I forgot...
Ответ:
The program written in Python is as follows
def GCD(num1, num2):
small = num1
if num1 > num2:
small = num2
for i in range(1, small+1):
if((num1 % i == 0) and (num2 % i == 0)):
gcd = i
print("The GCD is "+ str(gcd))
def LCM(num1,num2):
big = num2
if num1 > num2:
big = num1
while(True):
if((big % num1 == 0) and (big % num2 == 0)):
lcm = big
break
big = big+1
print("The LCM is "+ str(lcm))
print("Enter two numbers: ")
num1 = int(input(": "))
num2 = int(input(": "))
GCD(num1, num2)
LCM(num1, num2)
Explanation:
This line defines the GCD function
def GCD(num1, num2):
This line initializes variable small to num1
small = num1
This line checks if num2 is less than num1, if yes: num2 is assigned to variable small
if num1 > num2:
small = num2
The following iteration determines the GCD of num1 and num2
for i in range(1, small+1):
if((num1 % i == 0) and (num2 % i == 0)):
gcd = i
This line prints the GCD
print("The GCD is "+ str(gcd))
This line defines the LCM function
def LCM(num1,num2):
This line initializes variable big to num2
big = num2
This line checks if num1 is greater than num2, if yes: num1 is assigned to variable big
if num1 > num2:
big = num1
The following iteration continues while the LCM has not been gotten.
while(True):
This if statement determines the LCM using modulo operator
if((big % num1 == 0) and (big % num2 == 0)):
lcm = big
break
big = big+1
This line prints the LCM of the two numbers
print("The LCM is "+ str(lcm))
The main starts here
This line prompts user for two numbers
print("Enter two numbers: ")
The next two lines get user inputs
num1 = int(input(": "))
num2 = int(input(": "))
This calls the GCD function
GCD(num1, num2)
This calls the LCM function
LCM(num1, num2)
See attachment for more structured program
Ответ: