CS LAB
6. Determine whether a number is a perfect number, an Armstrong number or a
palindrome.
CODE:
# Function to check if a number is a perfect number
def is_perfect_number(n):
divisors_sum = sum(i for i in range(1, n) if n % i == 0)
return divisors_sum == n
# Function to check if a number is an Armstrong number
def is_armstrong_number(n):
digits = str(n)
num_digits = len(digits)
return n == sum(int(digit) ** num_digits for digit in digits)
# Function to check if a number is a palindrome
def is_palindrome(n):
return str(n) == str(n)[::-1]
# Main function to check all properties
def check_number_properties(n):
if is_perfect_number(n):
print(f"{n} is a Perfect number.")
else:
print(f"{n} is not a Perfect number.")
if is_armstrong_number(n):
print(f"{n} is an Armstrong number.")
else:
print(f"{n} is not an Armstrong number.")
if is_palindrome(n):
print(f"{n} is a Palindrome.")
else:
print(f"{n} is not a Palindrome.")
# Input from user
num = int(input("Enter a number: "))
check_number_properties(num)
7. Input a number and check if the number is a prime or composite number.
CODE:
# Function to check if a number is prime or composite
def check_number_type(n):
if n <= 1:
print(f"{n} is neither prime nor composite.")
elif n == 2:
print(f"{n} is a Prime number.")
else:
for i in range(2, n):
if n % i == 0:
print(f"{n} is a Composite number.")
return
print(f"{n} is a Prime number.")
# Input from user
num = int(input("Enter a number: "))
check_number_type(num)
8. Display the terms of a Fibonacci series.
CODE:
# Function to display Fibonacci series
def fibonacci_series(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
# Input from user
terms = int(input("Enter the number of terms for Fibonacci series: "))
fibonacci_series(terms)
9. Compute the greatest common divisor and least common multiple of two
integers.
CODE:
# Function to compute GCD
def gcd(a, b):
while b:
a, b = b, a % b
return a
# Function to compute LCM
def lcm(a, b):
return abs(a * b) // gcd(a, b)
# Input from user
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# Calculate and display GCD and LCM
print(f"The GCD of {num1} and {num2} is: {gcd(num1, num2)}")
print(f"The LCM of {num1} and {num2} is: {lcm(num1, num2)}")
___________________________________________________________________
10.Count and display the number of vowels, consonants, uppercase,
Lowercase characters in string.
CODE:
# Function to count vowels, consonants, uppercase and lowercase characters
def count_characters(s):
vowels = "aeiouAEIOU"
vowels_count = consonants_count = uppercase_count = lowercase_count = 0
for char in s:
if char.isalpha(): # Check if the character is a letter
if char in vowels:
vowels_count += 1
else:
consonants_count += 1
if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1
print(f"Vowels: {vowels_count}")
print(f"Consonants: {consonants_count}")
print(f"Uppercase characters: {uppercase_count}")
print(f"Lowercase characters: {lowercase_count}")
# Input from user
input_string = input("Enter a string: ")
count_characters(input_string)