Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

python

The document contains a collection of Python programs that demonstrate various functionalities such as a simple calculator, Fibonacci series, palindrome checker, prime number checker, and more. Each program includes code snippets and example outputs to illustrate their usage. The programs cover fundamental programming concepts and algorithms, making it a useful resource for learning Python.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python

The document contains a collection of Python programs that demonstrate various functionalities such as a simple calculator, Fibonacci series, palindrome checker, prime number checker, and more. Each program includes code snippets and example outputs to illustrate their usage. The programs cover fundamental programming concepts and algorithms, making it a useful resource for learning Python.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Index of Titles

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")

choice = input("Enter choice (1/2/3/4): ")

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))

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):

Enter the number of terms: 6


Fibonacci Series: 0 1 1 2 3 5
3. Check for Palindrome

Code:

# Check for Palindrome


string = input("Enter a string: ")
if string == string[::-1]:
print(f"{string} is a Palindrome")
else:
print(f"{string} is not a Palindrome")

Output (Example):

csharp
Copy code
Enter a string: madam
madam is a Palindrome
4. Prime Number Checker

Code:

# Prime Number Checker


num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print(f"{num} is not a prime number")
break
else:
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")

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)

num = int(input("Enter a number: "))


print(f"The factorial of {num} is {factorial(num)}")

Output (Example):

csharp
Copy code
Enter a number: 5
The factorial of 5 is 120

6. Count Vowels in a String


Code:

# 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

7. Sum of Digits of a Number

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

8. Generate Multiplication Table

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

9. Check Armstrong Number

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

10. Linear Search

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

for i, value in enumerate(arr):


if value == key:
print(f"Element {key} found at index {i}")
found = True
break

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

11. Binary Search

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

arr = sorted(list(map(int, input("Enter sorted elements separated by


spaces: ").split())))

key = int(input("Enter the element to search: "))


index = binary_search(arr, key)

if index != -1:
print(f"Element {key} found at index {index}")
else:
print(f"Element {key} not found")

Output (Example):

Enter sorted elements separated by spaces: 10 20 30 40 50


Enter the element to search: 30
Element 30 found at index 2

12. Bubble Sort


Code:

# Bubble Sort
arr = list(map(int, input("Enter the elements of the array separated by
spaces: ").split()))
n = len(arr)

for i in range(n - 1):


for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

print("Sorted array:", arr)

Output (Example):

Enter the elements of the array separated by spaces: 64 34 25 12 22 11 90


Sorted array: [11, 12, 22, 25, 34, 64, 90]

13. File Handling - Read and Write

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.")

# Reading from file


with open(filename, "r") as file:
content = file.read()

print("Content of the file:")


print(content)

Output (Example):

Content of the file:


Hello, this is a test file.
Welcome to file handling in Python.

14. Create and Use a Dictionary

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

15. Create a Student Grading System

Code:

# Grading System
marks = int(input("Enter marks: "))

if marks >= 90:


grade = 'A'
elif marks >= 75:
grade = 'B'
elif marks >= 60:
grade = 'C'
elif marks >= 50:
grade = 'D'
else:
grade = 'F'

print(f"Marks: {marks}, Grade: {grade}")

Output (Example):

Enter marks: 85
Marks: 85, Grade: B

You might also like