Assignments
4. Write a program in python to check a given number is Armstrong or not.
An Armstrong number (or narcissistic number) is a number that is equal to the sum of its own digits,
each raised to the power of the total number of digits in the number.
For example, 153 is an Armstrong number because it has three digits, and 1³ + 5³ + 3³ = 1 + 125 + 27 =
153.
1634 is an example of 4-digit Armstrong number.
14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
Program Logic :
1. Find the number of digits in a given number: using len() function of string
2. Initialize a variable sum to zero.
3. Extract each digit from the given number and raise it to the power of the number of digits
and add it to the sum. (extract each digit by % modulo and // floor division operator, use
power ** operator to find power of each digit and go on adding it to sum every time)
4. If the sum is equal to the given number, then it is an Armstrong number, else it is not.
# program in python to check a given number is Armstrong or not.
# Accept number to check from the user
num = int(input("Enter a number: "))
# Change num variable to string & calculate the length (number of digits)
order = len(str(num))
# initialize sum
sum = 0
# extract each digit and find the sum of the powers of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# display the result
1
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output
Enter a number: 1634
1634 is an Armstrong number
Enter a number: 135
135 is not an Armstrong number
2
Assignments
5. Write a program in python to find twin primes in a given range.
Twin primes are pairs of prime numbers that differ by 2.
Examples: (3, 5), (5, 7), (11, 13), (17, 19)
SymPy is a Python library for symbolic mathematics.
# Twin primes in given range using sympy.isprime() function
import sympy
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
print("Twin prime numbers are")
for i in range(start, end-1): # end-1 because we are checking i and i+2
if sympy.isprime(i) and sympy.isprime(i+2):
print(i, "and", i+2)
Output
Enter the starting number: 1
Enter the ending number: 30
Twin prime numbers are
3 and 5
5 and 7
11 and 13
17 and 19
6. Write a program in python to calculate LCM of two given numbers
# math.lcm() function calculates the least common multiple of two or more integers.
import math
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
lcm = math.lcm(a, b)
print("LCM:", lcm)
Output
Enter first number: 8
Enter second number: 12
LCM: 24
7. Write a program in python to calculate GCD of two given numbers
# math.gcd () function calculates the greatest common divisor of two or more integers.
import math
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
3
print("GCD:", math.gcd(a, b))
Output
Enter first number: 8
Enter second number: 12
GCD: 4
8. Write a program in python to display first N terms of the Fibonacci sequence.
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding
ones, typically starting with 0 and 1.
#print first N terms of Fibonacci sequence.
n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
print(a, end=" ")
nth = a + b
a=b
b = nth
Output
Enter number of terms: 8
0 1 1 2 3 5 8 13
9. Write a program in python to calculate factors of a given number
num = int(input("Enter number: "))
for i in range(1, num + 1):
if num % i == 0:
print(i, end=" ")
Output
Enter number: 24
1 2 3 4 6 8 12 24
10. Write a program in python to calculate prime factors of a given number
The prime factors of a number are all the prime numbers that, when multiplied together, equal the
original number.
Eg the prime factors of 12 are 2, 2, and 3 because 2 x 2 x 3 = 12.
num = int(input("Enter number: "))
i=2
while i <= num:
if num % i == 0:
print(i, end=" ")
num = num // i
else:
i=i+1
Output
Enter number: 24
2 2 2 3
4
11. Write a program in python to Multiply Two Matrix.
import numpy as np
A = np.array([[1,2], [3, 4]])
B = np.array([[2, 0], [1, 2]])
result_dot = np.dot(A,B) # dot product
result_matmul = np.matmul(A,B) # matmul() function
result_at_operator = A @ B # @ operator
print(result_dot)
print(result_matmul)
print(result_at_operator)
Output # We get same output for above mentioned methods:
[[ 4 4]
[10 8]]
5
12. Write a program in python to create a list, insert data, sort them and display the list.
13. Write a program in python to create a list, insert data, and find minimum & maximum value
from the list.
14. Write a program in python to create a list, insert data, and search for a given value from the
list.
6
15. Write a program in python to create a list, insert data, and calculate frequency (occurrence) of
a given value from the list.
7
16. Write a program in python to append new string with an existing string.
1. Using the + Operator:
2. Using the += Operator (Augmented Assignment):
3. Using the join() Method:
4. Using f-strings (Formatted String Literals - Python 3.6+):
17. Write a program in python to Count all letters, digits, and special symbols from a given string.
18. Write a program in python to reverse a given string
8
9