2nd Programs unit-2
2nd Programs unit-2
Output: Output:
Enter a number: 4 The sum of the numbers is: 15
4 is even.
3. Count Down Using While Loop 4. Find the Maximum Number in a
List
start_number = int(input("Enter a number
to count down from: ")) numbers = [10, 50, 30, 20, 40]
while start_number >= 0:
max_number = numbers[0]
print(start_number)
for number in numbers:
start_number -= 1
if number > max_number:
Output: max_number = number
Enter a number to count down from: 3 print("The maximum number is:",
max_number)
3
2
1 Output:
0 The maximum number is: 50
5. Dictionary to Store Student Grades 6. FizzBuzz Output
1
2
grades = {
for i in range(1, 101): Fizz
"Alice": 85, 4
if i % 3 == 0 and i %
"Bob": 92, Buzz
5 == 0: Fizz
"Charlie": 78,
print("FizzBuzz") 7
}
8
for student, grade in grades.items(): elif i % 3 == 0:
Fizz
print(f"{student}: {grade}") print("Fizz") Buzz
elif i % 5 == 0: 11
Fizz
Output: print("Buzz") 13
Alice: 85 else: 14
Bob: 92 FizzBuzz
print(i)
Charlie: 78 ...
Question:
Write a program that converts a temperature from Celsius to Fahrenheit.
The program should prompt the user to enter a temperature in Celsius and
then display the equivalent temperature in Fahrenheit.
Output:
Enter temperature in Celsius: 25
25.0°C is equal to 77.0°F
Question:
Write a program that checks if a given word is a palindrome (a word that reads the
same backward as forward). The program should prompt the user to enter a word
and display whether it is a palindrome.
# Program to check if a word is a palindrome
word = input("Enter a word: ")
if word == word[::-1]:
print(f"{word} is a palindrome.")
else:
print(f"{word} is not a palindrome.")
Output:
Enter a word: radar
radar is a palindrome.
(If you enter "hello", the output will be "hello is not a palindrome.")
Question:
Write a program that calculates the factorial of a number entered by the user. The
program should prompt the user for a non-negative integer and print the factorial.
Output:
Enter a non-negative integer: 5
The factorial of 5 is 120.
Question:
Write a program that takes a list of numbers and prints only the unique elements.
The program should define a list and iterate through it to find unique values.
Output:
Unique elements: [1, 2, 3, 4, 5]
Question:
Write a program that simulates a simple voting system. The program should allow users to vote for one of three
candidates and display the total votes for each candidate at the end.
Output:
# Program to generate a multiplication table Enter a number: 5
number = int(input("Enter a number: ")) Multiplication table for 5:
5x1=5
5 x 2 = 10
print(f"Multiplication table for {number}:")
5 x 3 = 15
for i in range(1, 11):
5 x 4 = 20
print(f"{number} x {i} = {number * i}")
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
Question:
Write a program that removes duplicates from a predefined list and prints the
resulting list.
# Program to remove duplicates from a list
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
Output:
List after removing duplicates: [1, 2, 3, 4, 5]
Question:
Write a program that creates a dictionary from two lists, where one list
contains keys and the other contains values.
# Program to create a dictionary from two lists
keys = ['name', 'age', 'city']
values = ['Alice', 30, 'New York']
Output:
Created dictionary: {'name': 'Alice', 'age': 30, 'city': 'New York'}
Question:
Write a program that checks if two given words are anagrams (words made by rearranging the
letters of another). The program should prompt the user to enter two words and display whether
they are anagrams.
Output:
Enter numbers separated by spaces: 10 20 30
The average is: 20.0
Question: Count Vowels in a String
Output:
Enter a string: Hello, World!
Question: Merge Two Lists
Output:
Merged list with unique elements: [1, 2, 3, 4, 5, 6]
Question: Count the Number of Words in a Sentence
Output:
Enter a sentence: Hello world, how are you?
The number of words in the sentence is: 6