Practical cs
Practical cs
OUTPUT
What is your name? Alice
# Compare the numbers and display the larger and smaller number
else:
OUTPUT
Enter the first number: 10
largest = num1
smallest = num1
largest = num2
largest = num3
smallest = num2
smallest = num3
OUTPUT
Enter the first number: 30
12
print("Pattern-2:")
1
# Pattern 2: Right-angled triangle with
numbers Pattern-3:
PYTHON PROGRAMME
#SERIES 1
total = 0
total += x**i
return total
#SERIES 2
total = 0
return total
#SERIES 3
total = 0
for i in range(2, n + 1):
total += x**i / i
return total
#SERIES 4
import math
total = 0
return total
OUTPUT
Enter the value of x: 2
if is_armstrong_number(num):
# Function to check if a number is an
Armstrong number print(f"{num} is an Armstrong Number.")
armstrong_sum = sum(int(digit) **
num_digits for digit in num_str) if is_palindrome(num):
else:
6 is a Perfect Number.
# Input number from the user
6 is not an Armstrong Number.
num = int(input("Enter a number: "))
6 is a Palindrome.
Practical 7
OBJECTIVE: Input a number and check if the number is
a prime or composite number.
PYTHON PROGRAMME
# Function to check if a number is prime or composite
def check_prime_or_composite(num):
if num <= 1:
if num % i == 0:
result = check_prime_or_composite(num)
OUTPUT
Enter a number: 29
Enter a number: 1
Enter a number: 15
def fibonacci(n):
for _ in range(n):
OUTPUT
Enter the number of terms for the Fibonacci series: 10
0 1 1 2 3 5 8 13 21 34
01123
Practical 9
OBJECTIVE: Compute the greatest common divisor and
least common multiple of two integers.
PYTHON PROGRAMME
# Function to compute the GCD of two numbers using the Euclidean algorithm
while b:
a, b = b, a % b # Update a and b
return a
# Function to compute the LCM of two numbers using the formula LCM(a, b) = |a * b| / GCD(a, b)
gcd_result = gcd(a, b)
lcm_result = lcm(a, b)
OUTPUT
Enter the first number: 12
vowels = "aeiouAEIOU"
vowels_count = 0
consonants_count = 0
uppercase_count = 0
lowercase_count = 0
if char in vowels:
else:
if char.isupper():
elif char.islower():
print(f"Vowels: {vowels_count}")
print(f"Consonants: {consonants_count}")
count_characters(input_string)
OUTPUT
Enter a string: Hello World!
Vowels: 3
Consonants: 7
Uppercase letters: 2
Lowercase letters: 8
Practical 11
OBJECTIVE: Input a string and determine whether it is
a palindrome or not; convert the case of characters in a
string.
PYTHON PROGRAMME
# Function to check if a string is a palindrome
def is_palindrome(input_string):
def convert_case(input_string):
return input_string.swapcase()
if is_palindrome(input_string):
else:
converted_string = convert_case(input_string)
OUTPUT
Enter a string: Hello
def find_largest_and_smallest(numbers):
# Input a list or tuple from the user (in this case, a list of numbers)
OUTPUT
Enter numbers separated by spaces: 30 18 42 10 15
def swap_even_odd_locations(numbers):
# Loop through the list, step 2 to check even and odd indexed elements
# Swap the elements at the current index (even) and the next index (odd)
return numbers
swapped_list = swap_even_odd_locations(numbers)
OUTPUT
Enter numbers separated by spaces: 1 2 3 4 5 6
List after swapping elements at even and odd locations: [20, 10, 40, 30, 50]
Practical 14
OBJECTIVE: Input a list or tuple of elements, search for
a given element in the list or tuple.
PYTHON PROGRAMME
# Function to search for an element in a list or tuple
if element in collection:
else:
if collection_type == "list":
else:
collection = []
print(result)
OUTPUT
Enter the collection type (list/tuple): tuple
def display_students_above_75(n):
student_dict = {}
for _ in range(n):
print(details['name'])
# Call the function to input data and display students with marks above 75
display_students_above_75(n)
OUTPUT
Enter the number of students: 3
Enter marks: 80
Enter marks: 70
Enter marks: 85
Alice
Charlie