python
python
1. Simple Calculator
2. Fibonacci Series
3. Check for Palindrome
4. Prime Number Checker
5. Factorial Calculation
6. Count Vowels in a String
7. Sum of Digits of a Number
8. Generate Multiplication Table
9. Check Armstrong Number
10. Linear Search
11. Binary Search
12. Bubble Sort
13. File Handling - Read and Write
14. Create and Use a Dictionary
15. Create a Student Grading System
1. Simple Calculator
Code:
# Simple Calculator
def calculator():
print("Select operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
if choice == '1':
print(f"The result is: {num1 + num2}")
elif choice == '2':
print(f"The result is: {num1 - num2}")
elif choice == '3':
print(f"The result is: {num1 * num2}")
elif choice == '4':
if num2 != 0:
print(f"The result is: {num1 / num2}")
else:
print("Error! Division by zero.")
else:
print("Invalid Input")
calculator()
Output (Example):
Select operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter choice (1/2/3/4): 1
Enter first number: 5
Enter second number: 3
The result is: 8.0
2. Fibonacci Series
Code:
# Fibonacci Series
n = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci Series:", end=" ")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
Output (Example):
Code:
Output (Example):
csharp
Copy code
Enter a string: madam
madam is a Palindrome
4. Prime Number Checker
Code:
Output (Example):
less
Copy code
Enter a number: 7
7 is a prime number
5. Factorial Calculation
Code:
# Factorial Calculation
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
Output (Example):
csharp
Copy code
Enter a number: 5
The factorial of 5 is 120
# Count Vowels
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = sum(1 for char in string if char in vowels)
print(f"The number of vowels in the string is {count}")
Output (Example):
csharp
Copy code
Enter a string: Hello
The number of vowels in the string is 2
Code:
python
Copy code
# Sum of Digits
num = int(input("Enter a number: "))
sum_digits = sum(int(digit) for digit in str(num))
print(f"The sum of digits of {num} is {sum_digits}")
Output (Example):
python
Copy code
Enter a number: 123
The sum of digits of 123 is 6
Code:
python
Copy code
# Multiplication Table
num = int(input("Enter a number: "))
print(f"Multiplication Table of {num}")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Output (Example):
less
Copy code
Enter a number: 5
Multiplication Table of 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Code:
python
Copy code
# Armstrong Number
num = int(input("Enter a number: "))
sum_digits = sum(int(digit) ** len(str(num)) for digit in str(num))
if num == sum_digits:
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")
Output (Example):
csharp
Copy code
Enter a number: 153
153 is an Armstrong number
Would you like the rest of the programs to be fully written out with code and output, or
should I summarize them for now?
3/3
4o
You said:
continue
ChatGPT said:
ChatGPT
Code:
python
Copy code
# Linear Search
arr = list(map(int, input("Enter the elements of the array separated by
spaces: ").split()))
key = int(input("Enter the element to search: "))
found = False
if not found:
print(f"Element {key} not found in the array")
Output (Example):
mathematica
Copy code
Enter the elements of the array separated by spaces: 10 20 30 40 50
Enter the element to search: 30
Element 30 found at index 2
Code:
# Binary Search
def binary_search(arr, key):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == key:
return mid
elif arr[mid] < key:
low = mid + 1
else:
high = mid - 1
return -1
if index != -1:
print(f"Element {key} found at index {index}")
else:
print(f"Element {key} not found")
Output (Example):
# Bubble Sort
arr = list(map(int, input("Enter the elements of the array separated by
spaces: ").split()))
n = len(arr)
Output (Example):
Code:
# File Handling
filename = "example.txt"
# Writing to file
with open(filename, "w") as file:
file.write("Hello, this is a test file.\nWelcome to file handling in
Python.")
Output (Example):
Code:
# Create Dictionary
student = {
"name": "John",
"age": 21,
"marks": {"Math": 85, "Science": 90, "English": 88}
}
# Access values
print("Name:", student["name"])
print("Age:", student["age"])
print("Marks in Science:", student["marks"]["Science"])
# Update value
student["age"] = 22
print("Updated Age:", student["age"])
Output (Example):
Name: John
Age: 21
Marks in Science: 90
Updated Age: 22
Code:
# Grading System
marks = int(input("Enter marks: "))
Output (Example):
Enter marks: 85
Marks: 85, Grade: B