0% found this document useful (0 votes)
2 views10 pages

Python Algorithms

The document outlines a series of algorithms for various programming tasks, including calculating sums, factorials, and managing user login attempts. It also covers string manipulation, data structures like lists and dictionaries, and file operations. Each program is presented in a clear, step-by-step format suitable for educational purposes.

Uploaded by

shreyasv628
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Python Algorithms

The document outlines a series of algorithms for various programming tasks, including calculating sums, factorials, and managing user login attempts. It also covers string manipulation, data structures like lists and dictionaries, and file operations. Each program is presented in a clear, step-by-step format suitable for educational purposes.

Uploaded by

shreyasv628
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 3a: Sum of First n Even Numbers

Algorithm

1. Start.

2. Input the value of n.

3. Initialize sum_even = 0.

4. Repeat from i = 1 to n.

5. Add 2 × i to sum_even.

6. Display the sum of the first n even numbers.

7. Stop.

Program 3b : Factorial of a Number

Algorithm

1. Start.

2. Input the number n.

3. Initialize factorial = 1.

4. Repeat from i = 1 to n.

5. Multiply factorial by i.
6. Display the factorial.

7. Stop.

Program 4(a): Login Attempt Limiter

Algorithm

1. Start.

2. Set the correct username and password.

3. Set maximum attempts to 3.

4. Repeat until attempts are exhausted.

5. Ask the user to enter username and password.

6. If both are correct:

o Display "Login Successful".

o Stop the loop.

7. Otherwise:

o Display "Invalid Credentials".

8. If all three attempts fail:

o Display "Account Locked".

9. Stop.

Program 4(b): Multiplication Tables (1 to 5)

Algorithm

1. Start.

2. Repeat from i = 1 to 5.

3. Display the table number.

4. Repeat from j = 1 to 10.

5. Display i × j = i × j.

6. Print a blank line after each table.

7. Stop.
Program 5(a): Count Vowels in a String

Algorithm

1. Start.

2. Input a string.

3. Initialize vowel count to 0.

4. Store all vowels (a, e, i, o, u in both uppercase and lowercase).

5. Traverse each character in the string.

6. If the character is a vowel:

o Increase the count by 1.

7. Display the total number of vowels.

8. Stop.

Program 5(b): Check Palindrome

Algorithm

1. Start.

2. Input a string.

3. Reverse the string.

4. Compare the original string with the reversed string.

5. If both are equal:

o Display "It is a palindrome."

6. Otherwise:

o Display "It is not a palindrome."

7. Stop.

Program 6(a): Student Marks List

Algorithm

1. Start.
2. Create an empty list for marks.

3. Repeat 5 times.

4. Input the marks of each student.

5. Store the marks in the list.

6. Find the highest mark.

7. Find the lowest mark.

8. Display the highest and lowest marks.

9. Stop.

Program 6(b): Grocery Inventory List

Algorithm

1. Start.

2. Create an empty grocery list.

3. Input the number of items to add.

4. Repeat until all items are entered.

5. Add each item to the grocery list.

6. Display the current grocery list.

7. Input the item to remove.

8. If the item exists:

o Remove it.

o Display "Item Removed."

9. Otherwise:

o Display "Item Not Found."

10. Display the updated grocery list.

11. Stop.

Program 7(a): Days of the Week (Tuple)

Algorithm

1. Start.
2. Store the names of the seven days in a tuple.

3. Display the heading "Days of the week:".

4. Traverse the tuple using a loop.

5. Display each day one by one.

6. Stop.

Program 7(b): Unique Numbers (Set)

Algorithm

1. Start.

2. Input numbers separated by spaces.

3. Convert the input string into a list of integers.

4. Convert the list into a set to remove duplicate numbers.

5. Display the unique numbers.

6. Stop.

Program 8(a): Weather Dictionary Builder

Algorithm

1. Start.

2. Create an empty dictionary called weather.

3. Input the number of cities.

4. Repeat for each city:

o Input the city name.

o Input the temperature.

o Store the city and temperature as a key-value pair in the dictionary.

5. Display the weather dictionary.

6. Stop.

Program 8(b): Word Frequency

Algorithm
1. Start.

2. Input a sentence.

3. Split the sentence into words.

4. Create an empty dictionary for word frequency.

5. Traverse each word in the sentence.

6. If the word already exists in the dictionary, increase its count by 1.

7. Otherwise, add the word with count 1.

8. Display the frequency of each word.

9. Stop.

Program 9(a): Electricity Bill Calculator

Algorithm

1. Start.

2. Define a function to calculate the electricity bill.

3. Input the number of units consumed.

4. If units are less than or equal to 100, calculate the bill at ₹5 per unit.

5. Else if units are less than or equal to 200, calculate:

o First 100 units at ₹5 per unit.

o Remaining units at ₹7 per unit.

6. Otherwise:

o First 100 units at ₹5 per unit.

o Next 100 units at ₹7 per unit.

o Remaining units at ₹10 per unit.

7. Display the total electricity bill.

8. Stop.

Program 9(b): Monthly Bills Analysis (Map, Filter, Reduce)

Algorithm
1. Start.

2. Input monthly electricity bills.

3. Convert the input into a list of integers.

4. Use map() to convert each bill from INR to USD.

5. Use filter() to select bills greater than ₹1000.

6. Use reduce() to calculate the total annual bill.

7. Display the bills in USD.

8. Display the bills above ₹1000.

9. Display the total annual bill.

10. Stop.

Program 10(a): Write to a File

Algorithm

1. Start.

2. Input text from the user.

3. Open the file [Link] in write mode.

4. Write the text into the file.

5. Close the file.

6. Display a success message.

7. Stop.

Program 10(b): Error Log Counter

Algorithm

1. Start.

2. Initialize the error count to 0.

3. Open the file [Link] in read mode.

4. Read the file line by line.

5. If a line contains the word "ERROR", increase the count by 1.


6. Close the file.

7. Display the total number of ERROR lines.

8. Stop.

Program 11(a): Validate Email

Algorithm

1. Start.

2. Input an email address.

3. Check whether the email contains "@" and ".".

4. If both are present, display "Valid Email".

5. Otherwise, display "Invalid Email".

6. Stop.

Program 11(b): Division with Error Handling

Algorithm

1. Start.

2. Input two numbers.

3. Input the arithmetic operation (+, -, *, /).

4. Use a try block to perform the operation.

5. If the operation is addition, subtraction, multiplication, or division, calculate the


result.

6. If the operation is invalid, display "Invalid operation choice."

7. If division by zero occurs, catch the ZeroDivisionError and display an error


message.

8. If the input is not numeric, catch the ValueError and display an error message.

9. If no exception occurs, display the result.

10. Stop.

These algorithms follow the same simple, exam-oriented format as the previous ones
and are suitable for practical record submissions and viva examinations.

You might also like