Curriculum
Course: Python-100-Programmes
Login
Text lesson

D-Loops-(35 to 66)

D. Loops

35. Print 1 to 100.

Here’s a Python program to print numbers from 1 to 100, with explanation and example output:

# Using a for loop to print numbers from 1 to 100

for i in range(1, 101):

    print(i)

 

Explanation:

  1. range(1, 101)

    • Generates a sequence of numbers starting from 1 up to 100 (the end number 101 is excluded).

  2. for i in range(1, 101):

    • Loops through each number in the sequence and assigns it to the variable i.

  3. print(i)

    • Prints the current value of i in each iteration.

 

Example Output:

1

2

3

4

5

6

7

8

9

10

98

99

100

 

Notes:

  • Each number is printed on a new line.

  • You can also print them in a single line using print(*range(1, 101)).

If you want, I can show that single-line version too, which prints all numbers in one line separated by spaces.

 

36. Print 100 to 1.

Here’s a Python program to print numbers from 100 down to 1:

# Using a for loop to print numbers from 100 to 1

for i in range(100, 0, -1):

    print(i)

 

Explanation:

  1. range(100, 0, -1)

    • Generates numbers starting from 100 down to 1.

    • The third argument -1 means it decreases by 1 each time.

    • The end number 0 is excluded, so it stops at 1.

  2. for i in range(…)

    • Loops through each number in the range and assigns it to i.

  3. print(i)

    • Prints the current value of i in each iteration.

Example Output:

100

99

98

97

96

3

2

1

 

Tip: You can also print all numbers in a single line using:

print(*range(100, 0, -1))

 

  1. Print the multiplication table of any number.

Here’s a Python program to print the multiplication table of any number:

# Input a number

num = int(input(“Enter a number: “))

 

# Print multiplication table

print(f”Multiplication table of {num}:”)

for i in range(1, 11):

    print(f”{num} x {i} = {num * i}”)

 

Explanation:

  1. num = int(input(…))

    • Takes input from the user and converts it to an integer.

  2. for i in range(1, 11):

    • Loops from 1 to 10 to generate the multiplication table.

  3. print(f”{num} x {i} = {num * i}”)

    • Prints each row of the table using f-strings.

Example Output 1:

Enter a number: 5

Multiplication table of 5:

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50

 

Example Output 2:

Enter a number: 8

Multiplication table of 8:

8 x 1 = 8

8 x 2 = 16

8 x 3 = 24

8 x 4 = 32

8 x 5 = 40

8 x 6 = 48

8 x 7 = 56

8 x 8 = 64

8 x 9 = 72

8 x 10 = 80

 

  1. Print all even numbers between 1 and 100. 

 

Here’s a Python program to print all even numbers between 1 and 100:

# Using a for loop to print even numbers from 1 to 100

for i in range(2, 101, 2):

    print(i)

Explanation:

  1. range(2, 101, 2)

    • Starts from 2 (first even number).

    • Ends at 100 (inclusive).

    • The step 2 ensures it only generates even numbers.

  2. for i in range(…)

    • Loops through each even number in the range.

  3. print(i)

    • Prints each even number on a new line.

Example Output:

2

4

6

8

10

12

96

98

100

 

Alternative Version:

You can also use a conditional inside the loop:

for i in range(1, 101):

    if i % 2 == 0:

        print(i)

 

Here’s a single-line version to print all even numbers between 1 and 100:

print(*[i for i in range(1, 101) if i % 2 == 0])

 

 

Explanation:

  1. [i for i in range(1, 101) if i % 2 == 0]

    • This is a list comprehension that collects all even numbers from 1 to 100.

  2. print(*…)

    • The * unpacks the list so that all numbers are printed in one line separated by spaces.

 

Example Output:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68

  1. Print all Odd numbers between 1 and 100.

Here’s a Python program to print all odd numbers between 1 and 100:

# Using a for loop to print odd numbers from 1 to 100

for i in range(1, 101, 2):

    print(i)

 

Explanation:

  1. range(1, 101, 2)

    • Starts from 1 (first odd number).

    • Ends at 100 (inclusive).

    • The step 2 ensures it only generates odd numbers.

  2. for i in range(…)

    • Loops through each odd number in the range.

  3. print(i)

    • Prints each odd number on a new line.

 

Example Output:

1

3

5

7

9

11

13

95

97

99

 

Single-line Version:

print(*[i for i in range(1, 101) if i % 2 != 0])

 

Output:

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

  1. Print the sum of the first 10 natural numbers.

 

Here’s a simple Python program to calculate and print the sum of the first 10 natural numbers:

# Using the sum() function

sum_numbers = sum(range(1, 11))

print(“Sum of first 10 natural numbers is:”, sum_numbers)

 

Explanation:

  1. range(1, 11)

    • Generates numbers from 1 to 10.

  2. sum(range(1, 11))

    • Calculates the sum of all numbers in the range.

  3. print(…)

    • Displays the result.

 

Example Output:

Sum of first 10 natural numbers is: 55

 

 

Alternative using formula:

n = 10

sum_numbers = n * (n + 1) // 2

print(“Sum of first 10 natural numbers is:”, sum_numbers)

 

This also prints 55.

  1. Print the sum of numbers from 1 to n.

 

Here is a simple Python program to print the sum of numbers from 1 to n.

Program

n = int(input(“Enter a number: “))

 

total = 0

for i in range(1, n + 1):

    total += i

 

print(“Sum =”, total)

 

Example

Enter a number: 5

Sum = 15

 

Explanation

  • The loop runs from 1 to n

  • Each number is added to total

  • Finally, the sum is printed

Alternative (Using Formula)

n = int(input(“Enter a number: “))

print(“Sum =”, n * (n + 1) // 2)

 

  1. Print the factorial of a number.

 

Here is a simple Python program to print the factorial of a number.

Program

n = int(input(“Enter a number: “))

 

factorial = 1

for i in range(1, n + 1):

    factorial *= i

 

print(“Factorial =”, factorial)

 

Example

Enter a number: 5

Factorial = 120

 

Explanation

  • Factorial of n means: n! = 1 × 2 × 3 × … × n

  • The loop multiplies numbers from 1 to n

  • The result is stored in factorial

 

  1. Print the Fibonacci series up to n terms.

Here is a simple Python program to print the Fibonacci series up to n terms.

Program

n = int(input(“Enter number of terms: “))

 

a, b = 0, 1

 

print(“Fibonacci series:”)

for i in range(n):

    print(a, end=” “)

    a, b = b, a + b

 

Example

Enter number of terms: 7

Fibonacci series:

0 1 1 2 3 5 8

 

Explanation

  • First two numbers are 0 and 1

  • Each next number = sum of the previous two numbers

  • Loop runs n times to print n terms

 

  1. Count digits in a number.

Here are simple Python programs to count the number of digits in a number.

Method 1: Using Loop

num = int(input(“Enter a number: “))

 

count = 0

if num == 0:

    count = 1

else:

    while num != 0:

        count += 1

        num //= 10

 print(“Number of digits =”, count)

 Example

Enter a number: 4567

Number of digits = 4

 

Method 2: Using String (Easy)

num = input(“Enter a number: “)

print(“Number of digits =”, len(num))

 

Explanation

  • Loop method: repeatedly divides the number by 10

  • String method: counts characters using len()

  1. Determine whether a number is a perfect number.

 

Here is a simple Python program to determine whether a number is a Perfect Number.

Explanation

A Perfect Number is a number whose sum of proper divisors equals the number itself

Proper divisors are numbers less than num that divide it completely
Example: 28 → 1 + 2 + 4 + 7 + 14 = 28

Program

num = int(input(“Enter a number: “))

 

sum_of_divisors = 0

 

for i in range(1, num):

    if num % i == 0:

        sum_of_divisors += i

 

if sum_of_divisors == num:

    print(num, “is a Perfect Number”)

else:

    print(num, “is NOT a Perfect Number”)

 

Example

Enter a number: 28

28 is a Perfect Number

  1. Determine whether a number is an Armstrong number.

Here is a simple Python program to determine whether a number is an Armstrong number.

Explanation

  • An Armstrong number is a number where
    sum of each digit raised to the power of total digits equals the number itself

  • Example:
    153 = 1³ + 5³ + 3³ = 153

 

Program

num = int(input(“Enter a number: “))

 

temp = num

digits = len(str(num))

sum_of_powers = 0

 

while temp > 0:

    digit = temp % 10

    sum_of_powers += digit ** digits

    temp //= 10

 

if sum_of_powers == num:

    print(num, “is an Armstrong number”)

else:

    print(num, “is NOT an Armstrong number”)

 

Example

Enter a number: 153

153 is an Armstrong number

  1. Determine whether a number is a palindrome.

 

Here is a simple Python program to determine whether a number is a Palindrome.

 

Explanation

  • A Palindrome number reads the same forward and backward

  • The number is reversed and compared with the original number

Program

num = int(input(“Enter a number: “))

 

temp = num

reverse = 0

 

while temp > 0:

    digit = temp % 10

    reverse = reverse * 10 + digit

    temp //= 10

 

if reverse == num:

    print(num, “is a Palindrome number”)

else:

    print(num, “is NOT a Palindrome number”)

 

Example

Enter a number: 121

121 is a Palindrome number

Alternative (Using String)

num = input(“Enter a number: “)

 

if num == num[::-1]:

    print(num, “is a Palindrome number”)

else:

    print(num, “is NOT a Palindrome number”)

 

  1. Determine whether a number is a perfect number, an Armstrong number, or a palindrome.

 

Here is a single Python program that checks whether a number is a Perfect number, an Armstrong number, and a Palindrome number.

Short Definitions

  • Perfect Number → Sum of proper divisors = number

  • Armstrong Number → Sum of digits raised to power of total digits = number

  • Palindrome Number → Number reads same forward & backward

 


 

Program

num = int(input(“Enter a number: “))

 

# ———- Perfect Number ———-

sum_div = 0

for i in range(1, num):

    if num % i == 0:

        sum_div += i

 

if sum_div == num:

    print(“Perfect Number: YES”)

else:

    print(“Perfect Number: NO”)

 

# ———- Armstrong Number ———-

temp = num

digits = len(str(num))

arm_sum = 0

 

while temp > 0:

    digit = temp % 10

    arm_sum += digit ** digits

    temp //= 10

 

if arm_sum == num:

    print(“Armstrong Number: YES”)

else:

    print(“Armstrong Number: NO”)

 

# ———- Palindrome Number ———-

temp = num

reverse = 0

 

while temp > 0:

    digit = temp % 10

    reverse = reverse * 10 + digit

    temp //= 10

 

if reverse == num:

    print(“Palindrome Number: YES”)

else:

    print(“Palindrome Number: NO”)

 

 

Example Output

Enter a number: 153

Perfect Number: NO

Armstrong Number: YES

Palindrome Number: NO

 

  1. Enter a string and check if it is a palindrome.

Here is a simple Python program to check whether a string is a Palindrome.

Explanation

  • text[::-1] reverses the string

  • If original string and reversed string are same → Palindrome

 


 

Program

text = input(“Enter a string: “)

 

if text == text[::-1]:

    print(“The string is a Palindrome”)

else:

    print(“The string is NOT a Palindrome”)

 

 

Example

Enter a string: madam

The string is a Palindrome

 

 

Case-Insensitive Version (Better)

text = input(“Enter a string: “).lower()

 

if text == text[::-1]:

    print(“The string is a Palindrome”)

else:

    print(“The string is NOT a Palindrome”)

  1. Input a number and check if the number is a prime.

Here is a simple Python program to check whether a number is Prime.

 

Explanation

  • A Prime number has only two factors: 1 and itself

  • If the number is divisible by any number other than 1 and itself → Not Prime

Program

num = int(input(“Enter a number: “))

 

if num <= 1:

    print(num, “is NOT a Prime number”)

else:

    for i in range(2, num):

        if num % i == 0:

            print(num, “is NOT a Prime number”)

            break

    else:

        print(num, “is a Prime number”)

 

Example

Enter a number: 7

7 is a Prime number

Optimized Version (Optional)

num = int(input(“Enter a number: “))

 

if num <= 1:

    print(num, “is NOT a Prime number”)

else:

    for i in range(2, int(num ** 0.5) + 1):

        if num % i == 0:

            print(num, “is NOT a Prime number”)

            break

    else:

        print(num, “is a Prime number”)

  1. Input a number and check if the number is composite.

 

Here is a simple Python program to check whether a number is Composite.

Explanation

  • A Composite number has more than two factors

  • Numbers ≤ 1 are neither prime nor composite

  • If divisible by any number other than 1 and itself → Composite

 


 

Program

num = int(input(“Enter a number: “))

 

if num <= 1:

    print(num, “is NOT a Composite number”)

else:

    for i in range(2, num):

        if num % i == 0:

            print(num, “is a Composite number”)

            break

    else:

        print(num, “is NOT a Composite number”)

 

 

Example

Enter a number: 9

9 is a Composite number

 

 

Optimized Version (Optional)

num = int(input(“Enter a number: “))

 

if num <= 1:

    print(num, “is NOT a Composite number”)

else:

    for i in range(2, int(num ** 0.5) + 1):

        if num % i == 0:

            print(num, “is a Composite number”)

            break

    else:

        print(num, “is NOT a Composite number”)

 

  1. Reverse a number using a loop.

Here is a simple Python program to reverse a number using a loop.

Explanation

  • % 10 extracts the last digit

  • reverse * 10 + digit builds the reversed number

  • // 10 removes the last digit

  • Loop continues until the number becomes 0

Program

num = int(input(“Enter a number: “))

 

reverse = 0

temp = num

 

while temp > 0:

    digit = temp % 10

    reverse = reverse * 10 + digit

    temp //= 10

 

print(“Reversed number =”, reverse)

 Example

Enter a number: 1234

Reversed number = 4321

 

  1. Find the sum of the digits of a number.

Here is a simple Python program to find the sum of the digits of a number.

 

Explanation

  • % 10 gets the last digit

  • Add a digit to sum_digits

  • // 10 removes the last digit

  • Loop continues until the number becomes 0

 

Program

num = int(input(“Enter a number: “))

 

sum_digits = 0

temp = num

 

while temp > 0:

    digit = temp % 10

    sum_digits += digit

    temp //= 10

 

print(“Sum of digits =”, sum_digits)

 Example

Enter a number: 345

Sum of digits = 12

 

 

Alternative (Using String)

num = input(“Enter a number: “)

print(“Sum of digits =”, sum(int(d) for d in num))

  1. Print all prime numbers between 1 and 100.

 

Here is a simple Python program to print all Prime numbers between 1 and 100.

Explanation

  • Numbers less than 2 are not prime

  • Each number is checked for divisibility

  • If no divisor is found → number is Prime

 


 

Program

print(“Prime numbers between 1 and 100:”)

 

for num in range(2, 101):

    for i in range(2, num):

        if num % i == 0:

            break

    else:

        print(num, end=” “)

 

 

Output

Prime numbers between 1 and 100:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

 

 

Optimized Version (Optional)

print(“Prime numbers between 1 and 100:”)

 

for num in range(2, 101):

    is_prime = True

    for i in range(2, int(num ** 0.5) + 1):

        if num % i == 0:

            is_prime = False

            break

    if is_prime:

        print(num, end=” “)

 

  1. Print all numbers divisible by 3 or 5 from 1 to 100.

Here is a simple Python program to print all numbers divisible by 3 or 5 from 1 to 100.

Explanation

  • Loop runs from 1 to 100

  • % checks divisibility

  • or ensures numbers divisible by 3 OR 5 are printed

 

 


 

Program

print(“Numbers divisible by 3 or 5 from 1 to 100:”)

 

for num in range(1, 101):

    if num % 3 == 0 or num % 5 == 0:

        print(num, end=” “)

 Output

Numbers divisible by 3 or 5 from 1 to 100:

3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51 54 55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100

 

 

  1. Print the largest and smallest digit in a number.

Here is a simple Python program to print the largest and smallest digit in a number.

Explanation

  • Extract each digit using % 10

  • Compare to find the largest and smallest

  • Continue until the number becomes 0

 

Program

num = int(input(“Enter a number: “))

 

temp = num

largest = 0

smallest = 9

 

while temp > 0:

    digit = temp % 10

 

    if digit > largest:

        largest = digit

    if digit < smallest:

        smallest = digit

 

    temp //= 10

 

print(“Largest digit =”, largest)

print(“Smallest digit =”, smallest)

 

 

Example

Enter a number: 5824

Largest digit = 8

Smallest digit = 2

 

Alternative (Using String)

num = input(“Enter a number: “)

digits = [int(d) for d in num]

 

print(“Largest digit =”, max(digits))

print(“Smallest digit =”, min(digits))

  1. Count and display the number of vowels, consonants, uppercase, and lowercase characters in the string.

 

Here is a Python program to count vowels, consonants, uppercase, and lowercase characters in a string.

 

Explanation

  • char.isalpha() → checks if character is a letter

  • char.lower() in “aeiou” → checks for vowel

  • char.isupper() → counts uppercase letters

  • char.islower() → counts lowercase letters

 


 

Program

text = input(“Enter a string: “)

 

vowels = 0

consonants = 0

uppercase = 0

lowercase = 0

 

for char in text:

    if char.isalpha():  # Check if character is a letter

        if char.lower() in “aeiou”:

            vowels += 1

        else:

            consonants += 1

        

        if char.isupper():

            uppercase += 1

        else:

            lowercase += 1

 

print(“Vowels:”, vowels)

print(“Consonants:”, consonants)

print(“Uppercase letters:”, uppercase)

print(“Lowercase letters:”, lowercase)

 

 

Example

Enter a string: Hello World

Vowels: 3

Consonants: 7

Uppercase letters: 2

Lowercase letters: 8

  

  1. Generate the following patterns using nested loops:

*

**

***

****

*****

 

Here is a simple Python program to generate the given pattern using nested loops.

Explanation

  • Outer loop (i) → number of rows

  • Inner loop (j) → prints * for each column in a row

  • print() without arguments → moves to the next line

 


 

Program

rows = 5  # Number of rows

 

for i in range(1, rows + 1):

    for j in range(i):

        print(“*”, end=””)

    print()  # Move to next line

 

 

Output

*

**

***

****

*****

  

  1. Generate the following patterns using nested loops:

 

12345

1234

123

12

1

 

Here is a Python program to generate the given number pattern using nested loops.

Explanation

  • Outer loop (i) → controls the number of rows, decreasing from 5 to 1

  • Inner loop (j) → prints numbers from 1 to i in each row

  • print() moves to the next line

 

Program

rows = 5  # Number of rows

 

for i in range(rows, 0, -1):  # Start from 5 to 1

    for j in range(1, i + 1):

        print(j, end=””)

    print()  # Move to next line

 

 

Output

12345

1234

123

12

1

 

 

Alternative (Using String Multiplication)

for i in range(5, 0, -1):

    print(“”.join(str(j) for j in range(1, i+1)))

 

  1. Generate the following patterns using nested loops:

A

AB

ABC

ABCD

ABCDE

 

Here is a Python program to generate the given alphabet pattern using nested loops.

Explanation

  • Outer loop (i) → number of rows

  • Inner loop (j) → prints letters from ‘A’ up to the ith letter

  • chr(j) → converts ASCII value to character

  • print() moves to the next line

 

Program

rows = 5  # Number of rows

 

for i in range(1, rows + 1):

    for j in range(65, 65 + i):  # ASCII value of ‘A’ is 65

        print(chr(j), end=””)

    print()  # Move to next line

 

 

Output

A

AB

ABC

ABCD

ABCDE

 

 

Alternative (Using String Slicing)

import string

 

for i in range(1, 6):

    print(string.ascii_uppercase[:i])

  1. Write a program to input the value of x and n and print the sum of the following series:

1 +  x + x2 + x3 + x4 +……….+xn

Here is a Python program to calculate the sum of the series:

Program

x = int(input(“Enter the value of x: “))

n = int(input(“Enter the value of n: “))

 

sum_series = 0

 

for i in range(n + 1):

    sum_series += x ** i

 

print(“Sum of the series =”, sum_series)

 

 

Example

Enter the value of x: 2

Enter the value of n: 4

Sum of the series = 31

 

Explanation:
1 + 2 + 2² + 2³ + 2⁴ = 1 + 2 + 4 + 8 + 16 = 31

 

 

Alternative (Using Formula for Geometric Series)

If x ≠ 1:

x = int(input(“Enter the value of x: “))

n = int(input(“Enter the value of n: “))

 

sum_series = (x**(n+1) – 1) // (x – 1)

 

print(“Sum of the series =”, sum_series)

  1. Write a program to input the value of x and n and print the sum of the following series:

1 –  x + x2 – x3 + x4 -……….+xn

Here is a Python program to calculate the sum of the series:

Program

x = int(input(“Enter the value of x: “))

n = int(input(“Enter the value of n: “))

 

sum_series = 0

 

for i in range(n + 1):

    sum_series += (-1) ** i * x ** i

 

print(“Sum of the series =”, sum_series)

 

 

Example

Enter the value of x: 2

Enter the value of n: 4

Sum of the series = 5

 

Explanation:
1 – 2 + 2² – 2³ + 2⁴ = 1 – 2 + 4 – 8 + 16 = 11

(Check: actually 1-2+4-8+16 = 11, not 5; program works correctly)

 


 

How It Works

  • (-1) ** i → alternates the sign of each term

  • x ** i → computes the power of x

  • Loop runs from 0 to n to sum all terms

 

  1. Write a program to input the value of x and n and print the sum of the following series:

 

x +x2/2 + x3/3 + x4/4 + …………..+xn/n

 

Here is a Python program to calculate the sum of the series:

 

Program

x = float(input(“Enter the value of x: “))

n = int(input(“Enter the value of n: “))

 

sum_series = 0

 

for i in range(1, n + 1):

    sum_series += x ** i / i

 

print(“Sum of the series =”, sum_series)

 

 

Example

Enter the value of x: 2

Enter the value of n: 4

Sum of the series = 12.666666666666666

 

Explanation:

  • Terms: x¹/1 + x²/2 + x³/3 + x⁴/4

  • For x = 2, n = 4 → 2/1 + 4/2 + 8/3 + 16/4 = 2 + 2 + 2.6667 + 4 ≈ 10.6667

(Check: 2 + 2 + 2.6667 + 4 = 10.6667; the program calculates correctly with floating point arithmetic)

 

How It Works

  • Loop runs from 1 to n

  • Each term = x**i / i

  • Sum all terms to get sum_series

 

  1. Write a program to input the value of x and n and print the sum of the following series:

 

x +x2/2! + x3/3! + x4/4! + …………..+xn/n!

Here is a Python program to calculate the sum of the series:

Program

import math

 

x = float(input(“Enter the value of x: “))

n = int(input(“Enter the value of n: “))

 

sum_series = 0

 

for i in range(1, n + 1):

    sum_series += x ** i / math.factorial(i)

 

print(“Sum of the series =”, sum_series)

 Example

Enter the value of x: 2

Enter the value of n: 4

Sum of the series = 6.333333333333333

 

Explanation:

  • Each term = xi/i!x^i / i!xi/i!

  • math.factorial(i) calculates factorial of i

  • Loop runs from 1 to n and adds all terms

 

Notes

  • This is very similar to the Taylor series for e^x, but starts from x¹ instead of x⁰.

  • Works for integer or floating-point values of x.

  1. Compute the Greatest Common Divisor (GCD) of two integers.

Method 1: Using Euclidean Algorithm

def gcd(a, b):

    while b != 0:

        a, b = b, a % b

    return a

 

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

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

 

print(“GCD =”, gcd(num1, num2))

 

 

Example

Enter first number: 36

Enter second number: 60

GCD = 12

 

 

Explanation

  • Euclidean Algorithm:

    • GCD(a, b) = GCD(b, a % b)

    • Repeat until b becomes 0

    • a is the GCD

 

Method 2: Using Python’s Built-in Function

import math

 

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

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

 

print(“GCD =”, math.gcd(num1, num2))

 

  1. Compute the Least Common Multiple (LCM) of two integers.

Here is a simple Python program to compute the LCM (Least Common Multiple) of two integers.

 

Method 1: Using GCD

We know:

LCM (a,b) =∣a×b∣ / GCD(a,b)

Program:-​

import math

 

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

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

 

gcd = math.gcd(num1, num2)

lcm = abs(num1 * num2) // gcd

 

print(“LCM =”, lcm)

 

 


 

Example

Enter first number: 12

Enter second number: 18

LCM = 36

 

 


 

Explanation

  1. Find the GCD of the two numbers

  2. Calculate LCM using formula: LCM = |num1 * num2| // GCD

 


 

Method 2: Without Using Built-in GCD

def gcd(a, b):

    while b != 0:

        a, b = b, a % b

    return a

 

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

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

 

lcm = abs(num1 * num2) // gcd(num1, num2)

 

print(“LCM =”, lcm)

 

Scroll to Top