n = int(input("Enter a positive integer: "))
temp = n
count = 0
digit_sum = 0
largest = 0
smallest = 9
while temp > 0:
digit = temp % 10
count += 1
digit_sum += digit
if digit > largest:
largest = digit
if digit < smallest:
smallest = digit
temp //= 10
print("Number of digits =", count)
print("Sum of digits =", digit_sum)
print("Largest digit =", largest)
print("Smallest digit =", smallest)Question 1
Write a Python program that accepts a positive integer and finds:
- the number of digits,
- the sum of its digits,
- the largest digit,
- the smallest digit.
Example
Input: 58327
Number of digits = 5
Sum of digits = 25
Largest digit = 8
Smallest digit = 2
Question 2
Write a Python program that accepts a sentence and counts the number of:
- uppercase letters,
- lowercase letters,
- digits,
- spaces,
- other characters.
Example
Input: Python Lab 2026!
Uppercase letters = 2
Lowercase letters = 7
Digits = 4
Spaces = 2
Other characters = 1
text = input("Enter a sentence: ")
upper = 0
lower = 0
digits = 0
spaces = 0
others = 0
for ch in text:
if ch.isupper():
upper += 1
elif ch.islower():
lower += 1
elif ch.isdigit():
digits += 1
elif ch.isspace():
spaces += 1
else:
others += 1
print("Uppercase letters =", upper)
print("Lowercase letters =", lower)
print("Digits =", digits)
print("Spaces =", spaces)
print("Other characters =", others)Question 3
Given a list of integers, find the second largest distinct number.
Example
Input:
[10, 40, 20, 40, 30]
Output:
Second largest = 30
numbers = [10, 40, 20, 40, 30]
largest = None
second_largest = None
for num in numbers:
if largest is None or num > largest:
if num != largest:
second_largest = largest
largest = num
elif num != largest and (second_largest is None or num > second_largest):
second_largest = num
print("Second largest =", second_largest)Question 4
Write a Python program that removes duplicate elements from a list without changing the order of the remaining elements.
Example
Input:
[4, 2, 4, 1, 2, 5, 1]
Output:
[4, 2, 1, 5]
numbers = [4, 2, 4, 1, 2, 5, 1]
result = []
for num in numbers:
if num not in result:
result.append(num)
print(result)