Tuesday, 13 February 2024

Class 11 CS Practical - 30

 

30 - Write a program in Python to input a string and determine whether the string a palindrome or not.

S=input("Enter a string: ")

if S ==  S[::-1]:

    print("String is Palindrome")

else:

    print("String is not Palindrome")



Class 11 CS Practical - 29

 

29 - Write a program in Python to computer the least common multiple of two integers.

import math

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

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

print("LCM of ",a,"and",b,"=",math.lcm(a,b))



Output:

Enter first number: 5
Enter second number: 7
LCM of  5 and 7 = 35

Enter first number: 8
Enter second number: 12
LCM of  8 and 12 = 24


Class 11 CS Practical - 28

 

28 - Write a program in Python to compute the greatest common divisor of two integers.

import math

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

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

print("GCD/HCF of ",a,"and",b,"=",math.gcd(a,b))


Output:

Enter first number: 60

Enter second number: 48

GCD/HCF of  60 and 48 = 12


Enter first number: 24

Enter second number: 18

GCD/HCF of  24 and 18 = 6




Class 11 CS Practical - 27

 

27 - Write a program in Python to input a number and determine whether the number is composite number or not.


Composite Numbers 

Composite Numbers are numbers that have more than two factors. These numbers are also called composites. Composite numbers are just the opposite of prime numbers which have only two factors, i.e. 1 and the number itself. All the natural numbers which are not prime numbers are composite numbers as they can be divided by more than two numbers. For example, 6 is a composite number because it is divisible by 1, 2, 3 and 6.

Examples:

4, 6, 8, 9, 10 etc.


n=int(input("Enter the number: ")) 

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

    if (n%i==0): 

        break 

if(n==i): 

    print(n,"is not a composite number") 

else: 

    print(n,"is a composite number")  


Output:

Enter the number: 4

4 is a composite number

Enter the number: 5

5 is not a composite number

Enter the number: 9

9 is a composite number



Class 11 CS Practical - 26

 

26 - Write a program in Python to to generate all prime numbers between 2 to 100.

print("Prime Number from 2 to 100: ")

for n in range(2,101):

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

        if (n%i==0): 

            break 

    if(n==i): 

        print(n,end=" ") 


Output:

Prime Number from 2 to 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


Class 11 CS Practical - 25

 

25 - Write a program in Python to input a number and determine whether the number is a prime number or not.

n=int(input("Enter the number: ")) 

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

    if (n%i==0): 

        break 

if(n==i): 

    print(n,"is a prime number") 

else: 

    print(n,"is not a prime number") 


Output:


Enter the number: 29

29 is a prime number

Enter the number: 37

37 is a prime number

Enter the number: 35

35 is not a prime number


Class 11 CS Practical - 24


24 - Write a program in Python to input a number and determine whether the number is a perfect number or not.

Perfect Number

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.

n=int(input("Enter the number: ")) 

sum=0 

for i in range(1,n): 

    if (n%i==0): 

        sum = sum+i 

if(sum==n): 

    print(n,"is a perfect number") 

else: 

    print(n,"is not a perfect number")


Output:

Enter the number: 6

6  is a perfect number

Enter the number: 25

25  is not a perfect number



Class 11 CS Practical - 23

 

23 - Write a program in Python to print all Armstrong numbers from 0 to 10000.


for n in range(0,10001):

    sum=0

    k=n

    while n>0:

        r=n%10

        n=n//10

        sum=sum+r*r*r

 

    if(sum==k):

        print(k,end=" ")


Output:

0 1 153 370 371 407 

Class 11 CS Practical - 22

 

22 - Write a program in Python to input a number and determine whether the number is an Armstrong number or not.


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

sum=0

k=n

while n>0:

    r=n%10

    n=n//10

    sum=sum+r*r*r

 

if(sum==k):

    print(k, "is a armstrong number")

else:

    print(k, "is not a armstrong number")


Output:

Enter a number: 153
153 is a armstrong number

Enter a number: 147
147 is not a armstrong number



Class 11 CS Practical - 21

 

21 - Write a program in Python to input a number and determine the multiplication of the digits of that number.

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

product=1

while n>0:

    r=n%10

    n=n//10

    product=product*r

print("Multiplication of digits=",product)


Output:

Enter a number: 145
Multiplication of digits= 20

Enter a number: 346
Multiplication of digits= 72