Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Info Tech

A. Design an algorithm to find the largest number in an array.


1. Initialize a variable `largest` to the first element of the array.
2. Iterate through the array from the second element to the last element:
- If the current element is greater than `largest`, update `largest` with
the current element.
3. After iterating through all elements, `largest` will hold the largest
number in the array.
4. Return `largest` as the result.

In Pseudocode form:
function findLargestNumber(array)
largest = array[0]
for i = 1 to length(array) - 1 do
if array[i] > largest then
largest = array[i]
end if
end for
return largest
end function
B. Design an algorithm to find the factorial of a number.
1. Initialize a variable `factorial` to 1.
2. Iterate from 1 to the given number (inclusive):
- Multiply `factorial` by the current number and update `factorial`.
3. After the loop, `factorial` will hold the factorial of the given number.
4. Return `factorial` as the result.

C. Design an algorithm to solve the Fibonacci sequence.


1. Initialize variables `a`, `b`, and `n` to 0, 1, and the desired position of
the Fibonacci sequence, respectively.
2. If `n` is 0, return 0 as the result.
3. If `n` is 1, return 1 as the result.
4. Iterate `n` times:
- Set `a` to the value of `b`, and `b` to the sum of `a` and the previous
value of `b`.
5. After the loop, `b` will hold the Fibonacci number at position `n`.
6. Return `b` as the result.

D. Design an algorithm to sort a list of numbers in ascending order.


1. Start with an unsorted list of numbers.
2. Repeat the following steps until the list is sorted:
- Set a boolean variable `swapped` to false.
- Iterate through the list from the first element to the second-to-last
element:
- If the current element is greater than the next element:
- Swap the current element with the next element.
- Set `swapped` to true.
- If `swapped` is still false after a complete iteration, the list is
already sorted, so break the loop.
3. The list is now sorted in ascending order.

E. Design an algorithm to check if a string is a palindrome.


1. Start with a string.
2. Initialize two pointers `start` and `end` to the beginning and end of the
string, respectively.
3. Repeat the following steps while `start` is less than or equal to `end`:
- If the characters at positions `start` and `end` are different, the string
is not a palindrome.
- Increment `start` and decrement `end`.
4. If the loop completes without finding any different characters, the
string is a palindrome.

F. Design an algorithm that prints all the prime numbers up to n.


1. Start with a number n.
2. Iterate from 2 to n (inclusive):
- Assume the current number is prime.
- Iterate from 2 to the square root of the current number (inclusive):
- If the current number is divisible by any number in this range,
mark it as not prime and break the loop.
- If the current number is still marked as prime, print it.

G. Design an algorithm to solve the quadratic equation.


1. Accept the coefficients a, b, and c of the quadratic equation.
2. Calculate the discriminant `D` using the formula: D = b^2 - 4ac.
3. If `D` is positive:
- Calculate the two roots of the quadratic equation using the
formulas:
- root1 = (-b + √D) / (2a)
- root2 = (-b - √D) / (2a)
- Return root1 and root2 as the results.
4. If `D` is zero:
- Calculate the single root of the quadratic equation using the
formula:
- root = -b / (2a)
- Return root as the result.
5. If `D` is negative, the quadratic equation has no real roots.
H. Design an algorithm that accepts a number and checks if it is even or
odd.
1. Accept a number.
2. Divide the number by 2.
3. If the remainder is 0, the number is even.
4. If the remainder is 1, the number is odd.

I. Design an algorithm to convert a binary number to a decimal


number.
1. Start with a binary number as a string.
2. Initialize a variable `decimal` to 0.
3. Iterate through the binary number string from right to left:
- Multiply the current digit (0 or 1) by 2 raised to the power of
its position.
- Add the result to `decimal`.
4. After iterating through all digits, `decimal` will hold the decimal
representation of the binary number.
5. Return `decimal` as the result.
J. Design an algorithm to calculate the area of a rectangle.
1. Accept the length and width of the rectangle.
2. Calculate the area using the formula: area = length * width.
3. Return the area as the result.

You might also like