0% found this document useful (0 votes)
0 views

algorithm

Uploaded by

cybertromero
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

algorithm

Uploaded by

cybertromero
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

ROMERO, CYBERT S.

BSIT 2-C

Task 1: Basic Calculator


Algorithm to perform basic arithmetic operations: addition, subtraction, multiplication, and division.

Step 1: Define the inputs:

- Input two numbers, num1 and num2.

- Input the desired operation: add, subtract, multiply, or divide.

Step 2: Check the operation:

- If the operation is add, calculate result = num1 + num2.

- If the operation is subtract, calculate result = num1 – num2.

- If the operation is multiply, calculate result = num1 * num2.

- If the operation is divide:

- Check if num2 is not 0.

- If num2 is not 0, calculate result = num1 / num2.

- Otherwise, display an error message: “Division by zero is not allowed.”

Step 3: Display the result of the operation.

Final Step: End the algorithm.

Task 2: Area of a Circle


Algorithm to calculate the area of a circle given the radius.

Step 1: Define the input:

- Input the radius of the circle, r.


Step 2: Calculate the area:

- Use the formula: area = π * r * r.

- (Take π as 3.14159).

Step 3: Display the calculated area.

Final Step: End the algorithm.

Task 3: Convert Fahrenheit to Celsius


Algorithm to convert temperature from Fahrenheit to Celsius.

Step 1: Define the input:

- Input the temperature in Fahrenheit, F.

Step 2: Convert Fahrenheit to Celsius:

- Use the formula: C = (F – 32) * 5 / 9.

Step 3: Display the temperature in Celsius, `C`.

Final Step: End the algorithm.

Task 4: Simple Interest


Algorithm to calculate simple interest.

Step 1: Define the inputs:


- Input the principal amount, P.

- Input the rate of interest, R (as a percentage).

- Input the time period, T (in years).

Step 2: Calculate the simple interest:

- Use the formula: SI = (P * R * T) / 100.

Step 3: Display the calculated simple interest, SI.

Final Step: End the algorithm.

Task 5: BMI Calculator


Algorithm to calculate Body Mass Index (BMI).

Step 1: Define the inputs:

- Input the weight of the person, weight (in kilograms).

- Input the height of the person, height (in meters).

Step 2: Calculate the BMI:

- Use the formula: BMI = weight / (height * height).

Step 3: Display the BMI value.

Step 4 (Optional): Provide a BMI classification:

- If BMI < 18.5, display “Underweight”.

- If 18.5 <= BMI <= 24.9, display “Normal weight”.

- If 25 <= BMI <= 29.9, display “Overweight”.


- If BMI >= 30, display “Obesity”.

Final Step: End the algorithm.

Task 6: Even or Odd


Algorithm to check whether a number is even or odd.

Step 1: Define the input:

- Input a number, num.

Step 2: Check if the number is even:

- If num % 2 == 0, then it is even.

- Otherwise, it is odd.

Step 3: Display the result (“Even” or “Odd”).

Final Step: End the algorithm.

Task 7: Leap Year Checker


Algorithm to check whether a year is a leap year.

Step 1: Define the input:

- Input the year, year.

Step 2: Check if the year is a leap year:

- If year % 4 == 0:
- Check if year % 100 == 0:

- If year % 400 == 0, it is a leap year.

- Otherwise, it is not a leap year.

- Otherwise, it is a leap year.

- Otherwise, it is not a leap year.

Step 3: Display the result (“Leap year” or “Not a leap year”).

Final Step: End the algorithm.

Task 8: Largest of Three Numbers


Algorithm to find the largest of three numbers.

Step 1: Define the inputs:

- Input three numbers, a, b, and c.

Step 2: Compare the numbers:

- If a > b and a > c, then a is the largest.

- Else if b > a and b > c, then b is the largest.

- Otherwise, c is the largest.

Step 3: Display the largest number.

Final Step: End the algorithm.

Task 9: Pass or Fail


Algorithm to check if a student passes or fails.

Step 1: Define the inputs:

- Input the score of the student, score.

- Define the passing score as 60.

Step 2: Check if the student passes:

- If score >= 60, display “Pass”.

- Otherwise, display “Fail”.

Final Step: End the algorithm.

Task 10: Voting Eligibility


Algorithm to check if a person is eligible to vote.

Step 1: Define the input:

- Input the age of the person, age.

Step 2: Check eligibility:

- If age >= 18, display “Eligible to vote”.

- Otherwise, display “Not eligible to vote”.

Final Step: End the algorithm.

Task 11: Print Multiplication Table


Algorithm to print the multiplication table of a number.
Step 1: Define the inputs:

- Input a number, num.

- Input the range, range_limit (e.g., 10).

Step 2: Initialize a counter:

- Set I = 1.

Step 3: Loop through the range:

- While I <= range_limit:

- Calculate result = num * i.

- Display the result in the format num x I = result.

- Increment i by 1.

Step 4: End the loop when I > range_limit.

Final Step: End the algorithm.

### **Task 12: Sum of Natural Numbers**

**Algorithm to find the sum of the first `n` natural numbers.**

**Step 1:** Define the input:

- Input a positive integer, `n`.

**Step 2:** Initialize the sum:

- Set `sum = 0`.

**Step 3:** Loop through the numbers:


- For `I = 1` to `n`:

- Add `i` to `sum`.

**Step 4:** Display the total sum.

**Final Step:** End the algorithm.

### **Task 13: Factorial of a Number**

**Algorithm to find the factorial of a number.**

**Step 1:** Define the input:

- Input a non-negative integer, `n`.

**Step 2:** Initialize the factorial:

- Set `factorial = 1`.

**Step 3:** Loop through the numbers:

- For `I = 1` to `n`:

- Multiply `factorial` by `i`.

**Step 4:** Display the calculated factorial.

**Final Step:** End the algorithm.

### **Task 14: Prime Number Checker**

**Algorithm to check if a number is prime.**


**Step 1:** Define the input:

- Input a number, `num`.

**Step 2:** Initialize a flag:

- Set `is_prime = true`.

**Step 3:** Check divisibility:

- If `num <= 1`, set `is_prime = false`.

- Otherwise, for `I = 2` to `sqrt(num)`:

- If `num % I == 0`, set `is_prime = false` and break.

**Step 4:** Display the result:

- If `is_prime = true`, display “Prime”.

- Otherwise, display “Not Prime”.

**Final Step:** End the algorithm.

### **Task 15: Fibonacci Sequence**

**Algorithm to print the first `n` Fibonacci numbers.**

**Step 1:** Define the input:

- Input the number of terms, `n`.

**Step 2:** Initialize the first two terms:

- Set `a = 0`, `b = 1`.

**Step 3:** Display the first two terms:

- Print `a` and `b`.


**Step 4:** Generate the sequence:

- For `I = 3` to `n`:

- Calculate `next_term = a + b`.

- Print `next_term`.

- Update `a = b` and `b = next_term`.

**Final Step:** End the algorithm.

### **Task 16: Sum of Array Elements**

**Algorithm to find the sum of all elements in an array.**

**Step 1:** Define the input:

- Input an array, `arr` of size `n`.

**Step 2:** Initialize the sum:

- Set `sum = 0`.

**Step 3:** Loop through the array:

- For each element `num` in `arr`:

- Add `num` to `sum`.

**Step 4:** Display the total sum.

**Final Step:** End the algorithm.

### **Task 17: Find Maximum in an Array**


**Algorithm to find the maximum element in an array.**

**Step 1:** Define the input:

- Input an array, `arr` of size `n`.

**Step 2:** Initialize the maximum:

- Set `max = arr[0]`.

**Step 3:** Loop through the array:

- For each element `num` in `arr`:

- If `num > max`, update `max = num`.

**Step 4:** Display the maximum value.

**Final Step:** End the algorithm.

### **Task 18: Reverse an Array**

**Algorithm to reverse the elements of an array.**

**Step 1:** Define the input:

- Input an array, `arr` of size `n`.

**Step 2:** Initialize two pointers:

- Set `start = 0` and `end = n – 1`.

**Step 3:** Swap elements:

- While `start < end`:

- Swap `arr[start]` and `arr[end]`.


- Increment `start` by 1.

- Decrement `end` by 1.

**Step 4:** Display the reversed array.

**Final Step:** End the algorithm.

### **Task 19: Sort an Array**

**Algorithm to sort an array in ascending order (using Bubble Sort).**

**Step 1:** Define the input:

- Input an array, `arr` of size `n`.

**Step 2:** Loop through the array:

- For `I = 0` to `n-1`:

- For `j = 0` to `n-i-2`:

- If `arr[j] > arr[j+1]`, swap `arr[j]` and `arr[j+1]`.

**Step 3:** Display the sorted array.

**Final Step:** End the algorithm.

### **Task 20: Count Even and Odd Numbers in an Array**

**Algorithm to count even and odd numbers in an array.**

**Step 1:** Define the input:

- Input an array, `arr` of size `n`.


**Step 2:** Initialize counters:

- Set `even_count = 0` and `odd_count = 0`.

**Step 3:** Loop through the array:

- For each element `num` in `arr`:

- If `num % 2 == 0`, increment `even_count`.

- Otherwise, increment `odd_count`.

**Step 4:** Display the counts.

**Final Step:** End the algorithm.

Here are the algorithms for **Tasks 21 to 49** divided into sections for readability:

## **Functions: Tasks 21–25**

### **Task 21: Simple Calculator Using Functions**

**Algorithm to perform basic arithmetic using functions.**

**Step 1:** Define four functions:

- `add(a, b)` → return `a + b`.

- `subtract(a, b)` → return `a – b`.

- `multiply(a, b)` → return `a * b`.

- `divide(a, b)` → return `a / b` (check if `b != 0`).

**Step 2:** Input two numbers, `num1` and `num2`.


**Step 3:** Input the operation type: `add`, `subtract`, `multiply`, or `divide`.

**Step 4:** Call the respective function and display the result.

**Final Step:** End the algorithm.

### **Task 22: Find the GCD**

**Algorithm to find the greatest common divisor (GCD) using the Euclidean method.**

**Step 1:** Define a function `gcd(a, b)`.

**Step 2:** In the function:

- While `b != 0`:

- Set `temp = b`.

- Update `b = a % b`.

- Update `a = temp`.

**Step 3:** Return `a` as the GCD.

**Final Step:** End the algorithm.

### **Task 23: Power of a Number**

**Algorithm to calculate the power of a number using a loop.**

**Step 1:** Define a function `power(base, exponent)`.


**Step 2:** Initialize `result = 1`.

**Step 3:** Loop `exponent` times:

- Multiply `result` by `base`.

**Step 4:** Return `result`.

**Final Step:** End the algorithm.

### **Task 24: Sum of Digits**

**Algorithm to calculate the sum of digits of a number.**

**Step 1:** Define a function `sum_of_digits(n)`.

**Step 2:** Initialize `sum = 0`.

**Step 3:** While `n > 0`:

- Add `n % 10` to `sum`.

- Update `n = n // 10`.

**Step 4:** Return `sum`.

**Final Step:** End the algorithm.

### **Task 25: Check Palindrome (String)**

**Algorithm to check if a string is a palindrome.**


**Step 1:** Define a function `is_palindrome(str)`.

**Step 2:** Compare the string with its reverse:

- If `str == str[::-1]`, return `True`.

- Otherwise, return `False`.

**Step 3:** Display the result.

**Final Step:** End the algorithm.

## **Strings: Tasks 26–30**

### **Task 26: Reverse a String**

**Algorithm to reverse a string.**

**Step 1:** Input a string, `str`.

**Step 2:** Reverse the string:

- Use slicing: `reversed_str = str[::-1]`.

**Step 3:** Display the reversed string.

**Final Step:** End the algorithm.

### **Task 27: Count Vowels in a String**

**Algorithm to count vowels in a string.**


**Step 1:** Input a string, `str`.

**Step 2:** Initialize `vowel_count = 0`.

**Step 3:** For each character in `str`:

- If the character is in `{‘a’, ‘e’, ‘I’, ‘o’, ‘u’}` (case insensitive), increment `vowel_count`.

**Step 4:** Display `vowel_count`.

**Final Step:** End the algorithm.

### **Task 28: String Palindrome**

(Same as Task 25.)

### **Task 29: Check Anagram**

**Algorithm to check if two strings are anagrams.**

**Step 1:** Input two strings, `str1` and `str2`.

**Step 2:** Sort both strings.

**Step 3:** Compare the sorted strings:

- If equal, they are anagrams.

**Step 4:** Display the result.

**Final Step:** End the algorithm.


### **Task 30: Convert Uppercase to Lowercase**

**Algorithm to convert uppercase letters in a string to lowercase.**

**Step 1:** Input a string, `str`.

**Step 2:** Convert to lowercase:

- Use `lowercase_str = str.lower()`.

**Step 3:** Display `lowercase_str`.

**Final Step:** End the algorithm.

## **File Handling: Tasks 31–35**

### **Task 31: Read a File**

**Algorithm to read the contents of a text file.**

**Step 1:** Input the file name, `file_name`.

**Step 2:** Open the file in read mode.

**Step 3:** Read and display its content.

**Final Step:** Close the file and end the algorithm.


### **Task 32: Write to a File**

**Algorithm to write content into a text file.**

**Step 1:** Input the file name, `file_name`.

**Step 2:** Input the content to write.

**Step 3:** Open the file in write mode and write the content.

**Final Step:** Close the file and end the algorithm.

### **Task 33: Word Count in a File**

**Algorithm to count the number of words in a text file.**

**Step 1:** Open the file in read mode.

**Step 2:** Read the content and split it into words.

**Step 3:** Count the words and display the result.

**Final Step:** End the algorithm.

### **Task 34: Copy Content from One File to Another**

**Algorithm to copy the content of one file into another.**

**Step 1:** Open the source file in read mode.


**Step 2:** Open the destination file in write mode.

**Step 3:** Copy content and save it to the destination file.

**Final Step:** End the algorithm.

### **Task 35: Find Longest Word in a File**

**Algorithm to find the longest word in a file.**

**Step 1:** Open the file and read its content.

**Step 2:** Split the content into words.

**Step 3:** Loop through the words and find the longest.

**Final Step:** End the algorithm.

Here are the algorithms for **Tasks 36 to 40**, focusing on recursion-based problems:

### **Task 36: Recursive Factorial**

**Algorithm to calculate the factorial of a number using recursion.**

**Step 1:** Define a function `factorial(n)`.


**Step 2:** Base Case:

- If `n == 0` or `n == 1`, return `1`.

**Step 3:** Recursive Case:

- Return `n * factorial(n-1)`.

**Step 4:** Input a non-negative integer, `n`.

**Step 5:** Call the function `factorial(n)` and display the result.

**Final Step:** End the algorithm.

### **Task 37: Recursive Fibonacci**

**Algorithm to calculate the nth Fibonacci number using recursion.**

**Step 1:** Define a function `fibonacci(n)`.

**Step 2:** Base Case:

- If `n == 1`, return `0`.

- If `n == 2`, return `1`.

**Step 3:** Recursive Case:

- Return `fibonacci(n-1) + fibonacci(n-2)`.

**Step 4:** Input the term number, `n`.

**Step 5:** Call the function `fibonacci(n)` and display the result.
**Final Step:** End the algorithm.

### **Task 38: Recursive Sum of Digits**

**Algorithm to calculate the sum of digits of a number using recursion.**

**Step 1:** Define a function `sum_of_digits(n)`.

**Step 2:** Base Case:

- If `n == 0`, return `0`.

**Step 3:** Recursive Case:

- Return `(n % 10) + sum_of_digits(n // 10)`.

**Step 4:** Input a non-negative integer, `n`.

**Step 5:** Call the function `sum_of_digits(n)` and display the result.

**Final Step:** End the algorithm.

### **Task 39: Recursive GCD**

**Algorithm to calculate the greatest common divisor (GCD) using recursion.**

**Step 1:** Define a function `gcd(a, b)`.

**Step 2:** Base Case:

- If `b == 0`, return `a`.


**Step 3:** Recursive Case:

- Return `gcd(b, a % b)`.

**Step 4:** Input two positive integers, `a` and `b`.

**Step 5:** Call the function `gcd(a, b)` and display the result.

**Final Step:** End the algorithm.

### **Task 40: Recursive Power**

**Algorithm to calculate the power of a number using recursion.**

**Step 1:** Define a function `power(base, exp)`.

**Step 2:** Base Case:

- If `exp == 0`, return `1`.

**Step 3:** Recursive Case:

- Return `base * power(base, exp – 1)`.

**Step 4:** Input the `base` and `exp` (exponent).

**Step 5:** Call the function `power(base, exp)` and display the result.

**Final Step:** End the algorithm.

Here are the algorithms for **Tasks 41 to 49**:


## **Miscellaneous: Tasks 41 to 45**

### **Task 41: Armstrong Number Checker**

**Algorithm to check if a number is an Armstrong number.**

**Step 1:** Define the input:

- Input a number, `num`.

**Step 2:** Initialize variables:

- Set `sum = 0`.

- Set `temp = num`.

**Step 3:** Extract digits and calculate the sum of their cubes:

- While `temp > 0`:

- Extract the last digit: `digit = temp % 10`.

- Add `digit^3` to `sum`.

- Remove the last digit: `temp = temp // 10`.

**Step 4:** Check if `sum == num`:

- If true, display “Armstrong Number”.

- Otherwise, display “Not an Armstrong Number”.

**Final Step:** End the algorithm.

### **Task 42: Palindrome Number**

**Algorithm to check if a number is a palindrome.**


**Step 1:** Define the input:

- Input a number, `num`.

**Step 2:** Initialize variables:

- Set `reversed_num = 0`.

- Set `temp = num`.

**Step 3:** Reverse the number:

- While `temp > 0`:

- Extract the last digit: `digit = temp % 10`.

- Update `reversed_num = reversed_num * 10 + digit`.

- Remove the last digit: `temp = temp // 10`.

**Step 4:** Check if `reversed_num == num`:

- If true, display “Palindrome Number”.

- Otherwise, display “Not a Palindrome Number”.

**Final Step:** End the algorithm.

### **Task 43: Count Prime Numbers**

**Algorithm to count all prime numbers between two given numbers.**

**Step 1:** Define the inputs:

- Input two numbers, `low` and `high`.

**Step 2:** Initialize `prime_count = 0`.


**Step 3:** Loop through the range:

- For each number `num` from `low` to `high`:

- Check if `num` is prime (use the prime-checking logic from Task 14).

- If `num` is prime, increment `prime_count`.

**Step 4:** Display `prime_count`.

**Final Step:** End the algorithm.

### **Task 44: Factorial Using Loop**

**Algorithm to calculate the factorial of a number using a loop.**

**Step 1:** Define the input:

- Input a number, `n`.

**Step 2:** Initialize `factorial = 1`.

**Step 3:** Loop through numbers:

- For `I = 1` to `n`:

- Multiply `factorial = factorial * i`.

**Step 4:** Display `factorial`.

**Final Step:** End the algorithm.

### **Task 45: Generate Prime Numbers**

**Algorithm to generate prime numbers up to a given limit.**


**Step 1:** Define the input:

- Input a number, `limit`.

**Step 2:** Loop through numbers:

- For each number `num` from `2` to `limit`:

- Check if `num` is prime (use the prime-checking logic from Task 14).

- If `num` is prime, display it.

**Final Step:** End the algorithm.

## **Advanced Loops & Patterns: Tasks 46 to 49**

### **Task 46: Print Star Pyramid**

**Algorithm to print a star pyramid pattern.**

**Step 1:** Define the input:

- Input the number of rows, `n`.

**Step 2:** Loop through rows:

- For each row `i` from `1` to `n`:

- Print `n-i` spaces.

- Print `2*i-1` stars.

**Final Step:** End the algorithm.

### **Task 47: Pascal’s Triangle**


**Algorithm to print Pascal’s triangle up to `n` rows.**

**Step 1:** Define the input:

- Input the number of rows, `n`.

**Step 2:** Loop through rows:

- For each row `i` from `0` to `n-1`:

- Initialize `value = 1`.

- Loop through columns `j` from `0` to `i`:

- Print `value`.

- Update `value = value * (I – j) / (j + 1)`.

**Step 3:** Print a newline after each row.

**Final Step:** End the algorithm.

### **Task 48: Diamond Pattern**

**Algorithm to print a diamond-shaped star pattern.**

**Step 1:** Define the input:

- Input the number of rows, `n`.

**Step 2:** Print the upper pyramid:

- For each row `i` from `1` to `n`:

- Print `n-i` spaces.

- Print `2*i-1` stars.

**Step 3:** Print the lower inverted pyramid:


- For each row `i` from `n-1` to `1`:

- Print `n-i` spaces.

- Print `2*i-1` stars.

**Final Step:** End the algorithm.

### **Task 49: Number Pattern**

**Algorithm to print a number pattern (e.g., triangle of numbers).**

**Step 1:** Define the input:

- Input the number of rows, `n`.

**Step 2:** Loop through rows:

- For each row `i` from `1` to `n`:

- Loop through columns `j` from `1` to `i`:

- Print `j`.

**Step 3:** Print a newline after each row.

**Final Step:** End the algorithm.

You might also like