Вы находитесь на странице: 1из 3

CHECK FOR PALINDROME

Aim:

To check whether a string is palindrome or not

Algorithm:

 Input the string.


 Find the reverse of the string.
 If the reverse of the string is equal to the input string, then return true. Else, return false.

Code:

# Python program to check whether a string is a palindrome or not

string = input(“Enter the string:”)


if(string == string[::-1]):
print(“The string is a palindrome”)
else:
print(“The string is not a palindrome”)

******************************************************************************

COMPUTE GCD AND LCM

Aim: To compute GCD and LCM of two numbers

Algorithm:

1. Get two values x and n


2. Call the function lcm which in turn will call the function gcd.
3. The gcd function will compute the value and return it to the function to lcm
4. The lcm function would return the value to the main program.
5. The main program would print the value

Code:

# Python Program to find GCD of Two Numbers

a = float(input(" Please Enter the First Value a: "))


b = float(input(" Please Enter the Second Value b: "))

i = 1
while(i <= a and i <= b):
if(a % i == 0 and b % i == 0):
gcd = i
i = i + 1

print("\n HCF of {0} and {1} = {2}".format(a, b, gcd))


lcm:

# Function to return
# LCM of two numbers
def findLCM(a, b):

lar = max(a, b)
small = min(a, b)
i = lar
while(1) :
if (i % small == 0):
return i
i += lar

# Driver Code
a = 5
b = 7
print("LCM of " , a , " and ",
b , " is " ,
findLCM(a, b), sep = "")

# Python program to find LCM of two numbers

# Recursive function to return gcd of a and b


def gcd(x,n):
if x == 0:
return n
return gcd(n % x, x)

# Function to return LCM of two numbers


def lcm(x,n):
return (x*n) / gcd(x,n)

# Driver program to test above function


x = int(input(“enter a number”))
n = int(input(“enter a number”))
print('LCM of', x, 'and', n, 'is', lcm(x, n))

*********************************************************************************************

ARMSTRONG NUMBER

Aim: To check whether the given number is Armstrong or not.


Armstrong number means : abcd... = an + bn + cn + dn + ...
Algorithm:

153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.

Code:
# Python program to check if the number provided by the user is an Armstrong
number or not

# take input from the user


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

# initialize sum
sum = 0

# find the sum of the cube of each digit


temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result


if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Вам также может понравиться