Docsity

Date 09/12/2019Program 1: Write a Python prog, Exercises of Microsoft Word Skills

Toggle sidebar
bg1
Date 09/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:
1. def gcd(x, y):
2. if x > y:
3. smaller = y
4. else:
5. smaller = x
6. for i in range(1,smaller + 1):
7. if((x % i == 0) and (y % i == 0)):
8. hcf = i
9. return hcf
10.
11. num1 = int(input("Enter first number: "))
12. num2 = int(input("Enter second number: "))
13. print("The GCD. of", num1,"and", num2,"is", gcd(num1, num2))
explanation
HCF: Highest Common Factor/ Greatest Common Divisor(GCD)
pf3

Partial preview of the text

Download Date 09/12/2019Program 1: Write a Python prog and more Exercises Microsoft Word Skills in PDF only on Docsity!

Date 09/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:

  1. def gcd(x, y):
  2. if x > y:
  3. smaller = y
  4. else :
  5. smaller = x
  6. for i in range( 1 ,smaller + 1 ):
  7. if ((x % i == 0 ) and (y % i == 0 )):
  8. hcf = i
  9. return hcf
  10. num1 = int(input("Enter first number: "))
  11. num2 = int(input("Enter second number: "))
  12. print ("The GCD. of", num1,"and", num2,"is", gcd(num1, num2)) explanation HCF: Highest Common Factor/ Greatest Common Divisor(GCD)

Highest Common Factor or Greatest Common Divisor of two or more integers when at least one of them is not zero is the largest positive integer that evenly divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4. For example: We have two integers 8 and 12. Let's find the HCF. The divisors of 8 are:

  1. 1 , 2 , 4 , 8 The divisors of 12 are:
  2. 1 , 2 , 3 , 4 , 6 , 12 HCF /GCD is the greatest common divisor. So HCF of 8 and 12 are 4.

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

Aim:

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

Program

  1. num = int(input("Enter a number: "))
  2. if num > 1 :
  3. for i in range( 2 ,num):
  4. if (num % i) == 0 :
  5. print (num,"is not a prime number")
  6. print (i,"times",num//i,"is",num)
  7. break
  8. else :
  9. print (num,"is a prime number")
  10. else :
  11. print (num,"is not a prime number") Prime numbers: