M.Sc Computer Application

M.Sc Python Programming Assignment 21

M.Sc Python Programming Assignments Assignment 21 Question: Write a Python program to find the greatest common divisor (gcd) of two integers  Code: def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) # Example usage num1 = int(input(“Enter the first integer: “)) num2 = int(input(“Enter the second integer: “)) gcd_result …

M.Sc Python Programming Assignment 21 Read More »

M.Sc Python Programming Assignment 20

M.Sc Python Programming Assignments Assignment 20 Question: Write a Python program to get the sum of a non-negative integer. Code: def sum_of_digits(n): if n < 10: return n else: return n % 10 + sum_of_digits(n // 10) number = int(input("Enter a non-negative integer: ")) sum_result = sum_of_digits(number) print("Sum of the digits:", sum_result) Output: Enter a …

M.Sc Python Programming Assignment 20 Read More »

M.Sc Python Programming Assignment 17

M.Sc Python Programming Assignments Assignment 17 Question: Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. Code: def factorial(n): if n == 0: return 1 else: return n * factorial(n – 1) number = int(input(“Enter a non-negative integer: “)) result = factorial(number) …

M.Sc Python Programming Assignment 17 Read More »

M.Sc Python Programming Assignment 16

M.Sc Python Programming Assignments Assignment 16 Question: Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters Code: def count_uppercase_lowercase(sentence): uppercase_count = 0 lowercase_count = 0 for char in sentence: if char.isupper(): uppercase_count += 1 elif char.islower(): lowercase_count += 1 return uppercase_count, lowercase_count sentence = input(“Enter …

M.Sc Python Programming Assignment 16 Read More »

M.Sc Python Programming Assignment 15

M.Sc Python Programming Assignments Assignment 15 Question: Write a program that accepts a sentence and calculate the number of letters and digits Code: def count_letters_digits(sentence): letter_count = 0 digit_count = 0 for char in sentence: if char.isalpha(): letter_count += 1 elif char.isdigit(): digit_count += 1 return letter_count, digit_count sentence = input(“Enter a sentence: “) letter_count, …

M.Sc Python Programming Assignment 15 Read More »

M.Sc Python Programming Assignment 14

M.Sc Python Programming Assignments Assignment 14 Question: Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalised. Code: def capitalize_lines(lines): capitalized_lines = [] for line in lines: capitalized_line = line.upper() capitalized_lines.append(capitalized_line) return capitalized_lines # Accept input lines print(“Enter lines (Press Enter to stop):”) …

M.Sc Python Programming Assignment 14 Read More »

M.Sc Python Programming Assignment 13

M.Sc Python Programming Assignments Assignment 13 Question: Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25])and makes a new list of only the first and last elements of the given list. Code: def extract_first_and_last(numbers): if len(numbers) >= 2: return [numbers[0], numbers[-1]] else: return numbers # Example …

M.Sc Python Programming Assignment 13 Read More »

Tech Amplifier Final Logo