0% found this document useful (0 votes)
5 views

easy level practice prgms

The document contains a series of Python programming solutions for various mathematical and computational problems. These include finding the smallest and largest elements in a list, checking for prime and Armstrong numbers, calculating the sum of digits, and performing matrix operations. Each solution is accompanied by example outputs demonstrating the functionality of the code.

Uploaded by

niranjanik.sse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

easy level practice prgms

The document contains a series of Python programming solutions for various mathematical and computational problems. These include finding the smallest and largest elements in a list, checking for prime and Armstrong numbers, calculating the sum of digits, and performing matrix operations. Each solution is accompanied by example outputs demonstrating the functionality of the code.

Uploaded by

niranjanik.sse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Write a python program to Find the Smallest and the Largest List
Elements.

Sol:

res_list = []

# prompting for the number of elements


num = int(input("How many elements in list? :"))

#loop to append each element entered by user to res_list


for x in range(num):
numbers = int(input('Enter number '))
res_list.append(numbers)

print("Maximum element in the list is :", max(res_list))


print("Minimum element in the list is :", min(res_list))

O/P:

How many elements in list? :5


Enter number 12
Enter number 45
Enter number 16
Enter number 10
Enter number 20
Maximum element in the list is : 45
Minimum element in the list is : 10

2.print prime no or not between 1 to 100.

Sol:

def checkPrime(num):

if num < 2:

return 0

else:

x = num // 2

for j in range(2, x + 1):

if num % j == 0:

return 0
return 1

a, b = 1, 100

for i in range(a, b + 1):

if checkPrime(i):

print(i, end=" ")

O/p:

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

3.check perfect no or not

Sol:

num = 25

sqrt_num = int(num**0.5)

if sqrt_num**2==num:

print("The number is a perfect square")

else:

print("The number is not a perfect square")

O/p:The number is a perfect square

4.perfect number no or not between 1 to 1000.

Sol:

def perfectSquares(l, r):

# For every element from the range

for i in range(l, r + 1):

if (i*(.5) == int(i*(.5))):
print(i, end=" ")

l=1

r = 100

perfectSquares(l, r)

Output:1 4 9 16 25 36 49 64 81 100

5. Check Armstrong or not

Sol:

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

sum = 0

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

if num == sum:

print(num,"is an Armstrong number")

else:

print(num,"is not an Armstrong number")

o/p:

Enter a number: 663

663 is not an Armstrong number

Enter a number: 407

407 is an Armstrong number


6.Check Armstrong number or not between 1 to 100.

Sol:

lower = 1

upper = 100

for num in range(lower, upper + 1):

# order of number

order = len(str(num))

# initialize sum

sum = 0

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** order

temp //= 10

if num == sum:

print(num)

O/P:

8
9

7.Check the given number is strong or not.

Sol:

sum=0

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

temp=num

while(num):

i=1

# fact variable with 1

fact=1

rem=num%10

while(i<=rem):

fact=fact*i

i=i+1

sum=sum+fact

num=num//10

if(sum==temp):

print("Given number is a strong number")

else:

print("Given number is not a strong number")

O/P: Enter a number:154

Given number is not a strong number

Enter a number:145
Given number is a strong number

8. Check Leap year or not

Sol:

year = 2020

if (year % 400 == 0) and (year % 100 == 0):

print("{0} is a leap year".format(year))

elif (year % 4 ==0) and (year % 100 != 0):

print("{0} is a leap year".format(year))

else:

print("{0} is not a leap year".format(year))

O/P: 2020 is a leap year

9. Convert Binary to Decimal

Sol:

num = 110

binary_val = num

decimal_val = 0

base = 1

while num > 0:

rem = num % 10

decimal_val = decimal_val + rem * base

num = num // 10

base = base * 2

print("Binary Number is {}\nDecimal Number is {}".format(binary_val, decimal_val))

O/p: Binary Number is 110

Decimal Number is 6
10.Convert Decimal to Binary

Sol:

def DecimalToBinary(num):

if num >= 1:

DecimalToBinary(num // 2)

print(num % 2, end = '')

if __name__ == '__main__':

dec_val = 24

DecimalToBinary(dec_val)

O/P: 011000

11. Find the LCM, GCD using python program

Sol:

import math

def my_lcm(x, y):

return (x * y) // math.gcd(x, y)

print(my_lcm(6, 4))

print(math.gcd(27, 18, 9))

O/P:

12

12. Reverse a Number

Sol:
num = 1234

reversed_num = 0

while num != 0:

digit = num % 10

reversed_num = reversed_num * 10 + digit

num //= 10

print("Reversed Number: " + str(reversed_num))

o/p: 4321

13. Find the Sum of Digits in a Number

Sol:

def getSum(n):

sum = 0

for digit in str(n):

sum += int(digit)

return sum

n = 12345

print(getSum(n))

o/p:15

14. Count the Number of Digits in a number

Sol:

num = 3452

count = 0

while num != 0:
num //= 10

count += 1

print("Number of digits: " + str(count))

o/p: Number of digits: 4

15.Find the Sum of N natural Numbers

Sol: num = 16

if num < 0:

print("Enter a positive number")

else:

sum = 0

while(num > 0):

sum += num

num -= 1

print("The sum is", sum)

o/p: The sum is 136

16.Check Composite or not

sol:

n=int(input('Enter the number '))


factor=0
for i in range(1,n):
if n%i==0:
factor=i
if factor>1:
print ('The number is a composite number!')
elif n==1:
print ('The number 1 is neither prime nor composite!')
else:
print ('This is not a composite number!')

17. Count the Vowels, Consonants in a String

Sol:

input_string = "Python Programming"

vowels = 'aeiouAEIOU'

consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'

vowel_count = 0

consonant_count = 0

for char in input_string:

if char in vowels:

vowel_count += 1

elif char in consonants:

consonant_count += 1

print(f"Vowels: {vowel_count}")

print(f"Consonants: {consonant_count}")

O/P: Vowels: 4

Consonants: 11

18. Add two Matrices (3*3 Matrix)

Sol: import numpy as np

matrix1 = np.array([[1, 2, 3],

[4, 5, 6],

[7, 8, 9]])

matrix2 = np.array([[9, 8, 7],

[6, 5, 4],

[3, 2, 1]])
result_matrix = np.add(matrix1, matrix2)

print(result_matrix)

O/P: [[10 10 10]

[10 10 10]

[10 10 10]]

19. Product of two matrices (3*3 Matrix)

Sol:

matrix1 = [[1, 2],

[3, 4]]

matrix2 = [[5, 6],

[7, 8]]

result = [[0, 0],

[0, 0]]

for i in range(len(matrix1)):

for j in range(len(matrix2[0])):

for k in range(len(matrix2)):

result[i][j] += matrix1[i][k] * matrix2[k][j]

print("Result Matrix:")

for row in result:

print(row)

O/P: Result Matrix:

[19, 22]

[43, 50]
20. Transpose of a Matrix.

Sol:

X = [[12,7],

[4 ,5],

[3 ,8]]

result = [[0,0,0],

[0,0,0]]

for i in range(len(X)):

for j in range(len(X[0])):

result[j][i] = X[i][j]

for r in result:

print(r)

O/P: [12, 4, 3]

[7, 5, 8]

You might also like