Docsity

Write a Python program to find GCD of two numbers, Lecture notes of Microprocessors

Toggle sidebar
bg1
Date 02/12/2019
Program 1: Write a Python program to find GCD of two numbers.
Aim:
To write a Python program to find GCD of two numbers.
Algorithm:
1. Define a function named compute GCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:
def computeGCD(x, y): if x > y:
smaller = y else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)): gcd = i
return gcd num1 = 54
num2 = 24
# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))
print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))
Sample Output:
$python main.py
pf3
pf4
pf5

Partial preview of the text

Download Write a Python program to find GCD of two numbers and more Lecture notes Microprocessors in PDF only on Docsity!

Date 02/12/

Program 1: Write a Python program to find GCD of two numbers.

Aim: To write a Python program to find GCD of two numbers. Algorithm:

1. Define a function named compute GCD() 2. Find the smallest among the two inputs x and y 3. Perform the following step till smaller+ Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i 4. Print the value of gcd

Program:

def computeGCD(x, y): if x > y: smaller = y else: smaller = x for i in range(1, smaller+1): if((x % i == 0) and (y % i == 0)): gcd = i return gcd num1 = 54 num2 = 24

take input from the user

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2)) Sample Output: $python main.py

  1. for num in range(0,n + 1), perform the following
  2. if num%i is 0 then break else print the value of num
  3. Repeat step 3 for i in range(2,num)

Program:

n = int(input("Enter the upper limit: ")) print("Prime numbers are") for num in range(0,n + 1):

prime numbers are greater than

1 if num > 1: Sample Output: $python main.py Enter the upper limit: 20 Prime numbers are 2 3 5 7 11 13 17 19 ('The GCD. of', 54, 'and', 24, 'is', 6)

Program 2: Write a Python program to find first n prime numbers.

Aim: To write a Python program to find first n prime numbers. Algorithm:

  1. Read the value of n

print("The maximum number is %d"%maxno)