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")
S=input("Enter a string: ")
if S == S[::-1]:
print("String is Palindrome")
else:
print("String is not Palindrome")
import math
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
print("LCM of ",a,"and",b,"=",math.lcm(a,b))
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
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
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
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
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
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=" ")
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")
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)