Correction and Outputs
Correction and Outputs
functional programming.
is not required.
Output
Program - 1
Example Output:
Enter the first number: 10
Enter the second number: 2
Arithmetic Operations:
10.0 + 2.0 = 12.0
10.0 - 2.0 = 8.0
10.0 * 2.0 = 20.0
10.0 / 2.0 = 5.0
10.0 % 2.0 = 0.0
Program - 2
Example Output:
Case 1 (Perfect Number):
Enter a number to check if it is perfect: 28
28 is a perfect number.
Explanation:
• 28 is a perfect number because its proper divisors are 1, 2, 4, 7, 14, and
their sum is 28.
• If the sum of proper divisors is not equal to the number, it is not a
perfect number.
Output
Program – 3
Example Output:
Case 1 (Armstrong Number):
Enter a number to check if it is an Armstrong number: 153
153 is an Armstrong number.
Explanation:
• 153 is an Armstrong number because 13+53+33=1531^3 + 5^3 + 3^3 =
15313+53+33=153.
• If the sum of the digits raised to the power of the number of digits is
not equal to the number, it is not an Armstrong number.
Program – 4
Example Output:
Case 1 (Positive Number):
Enter a number to find its factorial: 5
The factorial of 5 is 120.
Case 1 (Zero):
Enter a number to find its factorial: 0
The factorial of 0 is 1.
Output
Case 3 (Negative Number):
Enter a number to find its factorial: -3
Factorial is not defined for negative numbers.
Explanation:
• Factorial of n is the product of all positive integers less than or equal
to n.
• n! = n × (n−1) × (n−2) ⋯ ×1
• For n=0, 0! is defined as 1.
Program – 5
Example Output:
Case 1 (Positive Number):
Enter the number of terms for the Fibonacci series: 7
The first 7 terms of the Fibonacci series are: [0, 1, 1, 2,
3, 5, 8]
Explanation:
• Fibonacci Series starts with 0 and 1, and each subsequent term is the
sum of the previous two terms: F(n)=F(n−1)+F(n−2)
• The series starts as: 0, 1, 1, 2, 3, 5, 8, ...
Output
Example Output:
Case 1 (Palindrome):
Enter a string to check if it is a palindrome: madam
"madam" is a palindrome.
Explanation:
1. Palindrome: A string is a palindrome if it reads the same backward as
forward.
2. Loop Logic: Compare characters from the start and end of the string
using a loop until the middle of the string is reached.
3. Case-insensitive check: Convert the string to lowercase to ensure the
comparison is not case-sensitive.
Output
Example Output:
Case 1: Numeric Input
Enter a list of elements separated by spaces: 5 3 8 2 5 10
Operations on the list:
Original list: [5, 3, 8, 2, 5, 10]
Length of the list: 6
Sorted list (if all items are numeric): [2, 3, 5, 5, 8, 10]
Maximum value (if numeric): 10
Minimum value (if numeric): 2
Reversed list: [10, 5, 2, 8, 3, 5]
Unique elements: [2, 3, 5, 8, 10]
Explanation:
• Input Handling: The input is split into individual elements and converted to
integers, floats, or kept as strings.
• Operations:
• Display the original list.
• Find the length of the list.
• Sort the list if it contains only numeric elements.
• Find the maximum and minimum values if numeric.
• Reverse the list.
• Display unique elements.
Output
Example Usage:
Input:
1. Enter file name: example.txt
2. Menu options allow you to:
• Read content.
• Append data to the file.
• Write data (overwriting existing content).
• View statistics like number of lines, words, and characters.
Example Output:
File Handling Utility in Python
Enter the file name: example.txt
Menu:
1. Read file content
2. Append data to the file
3. Write data to the file
4. Show file statistics
5. Exit
Enter your choice (1-5): 1
File Content:
Hello, this is a sample file.
Menu:
Enter your choice (1-5): 4
File Statistics:
Number of lines: 1
Number of words: 6
Number of characters: 30
Key Features:
1. Read file content: Display the content of the file.
2. Append data: Add new content without overwriting existing data.
3. Write data: Overwrite the file with new data.
4. File statistics: Count lines, words, and characters in the file.
5. Error handling: Handles cases where the file doesn't exist or other unexpected
errors occur.
Output
Example Output:
Case 1: File with Content
Suppose the file example.txt contains the following content:
Input:
Enter the file name: example.txt
Enter the word to count occurrences of: file
Output:
The word "file" occurs 3 times in the file "example.txt".
Output:
Error:the
Enter Thefile
filename:
'nonexistent.txt'
nonexistent.txt
does not exist.
Enter the word to count occurrences of: file
Output
Example Output:
Case 1: Word with vowels
Input:
Enter a word: beautiful
Output:
Unique vowels in 'beautiful' are: ['e', 'a', 'u', 'i']
Output:
No vowels found in 'rhythm'.
Explanation:
1. Stack Usage: The program uses a list as a stack to store vowels. Only unique
vowels are added to the stack.
2. Case Insensitivity: The input word is converted to lowercase to ensure vowels are
matched correctly regardless of case.
3. Logic:
• Loop through each character in the word.
• If the character is a vowel (a, e, i, o, u) and not already in the stack, push it
onto the stack.
4. Result: The stack contains all the unique vowels in the order they appear in the
word.
Output
Example Usage:
Case 1: Adding Records
Student Records:
Roll No Name Grade
------------------------------
101 John Doe A
Explanation:
1. File Handling: The program uses the student_records.txt file to store
and retrieve student records.
2. Modular Design: Each operation (add, view, search, delete) is
implemented as a separate function for clarity.
3. Error Handling: Ensures the program doesn’t crash if the file doesn’t
exist or if invalid input is provided.
Output
Example Input/Output:
Enter the number up to which prime numbers are to be
generated: 20
Prime numbers up to 20: [2, 3, 5, 7, 11, 13, 17, 19]
Output
Example Input/Output:
Case 1: Two Positive Numbers
Input:
Enter the first number: 56
Enter the second number: 98
Output:
The GCD of 56 and 98 is: 14
Output:
The GCD of 0 and 25 is: 25
Explanation:
1. Euclidean Algorithm:
• The GCD of two numbers a and b is calculated by repeatedly replacing a
with b and b with a % b (remainder of a divided by b) until b becomes 0.
• At this point, a contains the GCD.
2. Handling Zero:
• If one number is zero, the GCD is the other number (e.g., GCD(0, 25) = 25).