Algorithms 17-38
Algorithms 17-38
1. Start
(a) Add the result of 'temp % 10' raised to the power of 'i' to 'sum'.
(a) Add the result of 'temp % 10' raised to the power of 'b' to 'sum1'.
12. Stop.
Algorithm 18
1. Start
- Case 1:
- Initialize `i` to 1.
- Repeat the following steps until `i` is less than or equal to `num`:
- Print `i*i`.
- Increment `i`.
- Print a newline.
- Go to 'repeat'.
- Case 2:
- Print "Exited\n".
- Default:
- Go to 'repeat'.
7. Return 0.
8. Stop.
Algorithm 19
1. Start
f. Decrement 'i' by 1.
5. Stop
Algorithm 20
1. Start
(a) Print three dots followed by three asterisks and then three dots on separate lines.
5. Stop
Algorithm 21
1. Start
8. Stop.
Algorithm 22
1. Start
4. Set `fact` to 1.
- Print "Factorial of a given number %d = %d\n" with `number` and the result of calling the
`factorial` function with `number` as arguments.
10. End.
Algorithm 23
1. Start
(a) Call the prime() function and check its return value.
(b) If the return value is 0, print "The given number is a prime number".
(c) If the return value is not 0, print "The given number is not a prime number".
5. Stop
Algorithm 24
1. Start
7. Stop
Function gcd:
1. Start
b. Set `y` to `x % y`
7. Stop
Algorithm 25
1. Start
(d) Calculate the factorial of `num` using the non-recursive function `factorial(num)` and store the
result in `fact`.
(f) Calculate the factorial of `num` using the recursive function `factorial_rec(num)` and store the
result in `fact_rec`.
(i) If the number is prime, display "Prime number"; otherwise, display "Not a prime number".
(i) Multiply `fac` by the loop variable `i` and update `fac`.
(i) If `n` is divisible by `i` without any remainder, add `i` to `sum`.
(c) If `sum` is equal to `n + 1`, return 1 (indicating that the number is prime); otherwise, return 0.
7. Stop.
Algorithm 26
1. Start
2. Read the value 'n' for the number of rows in Pascal's triangle.
3. Define a function `factorial` that takes an integer `x` as input and returns its factorial.
5. Define a function `pascal` that takes an integer `n` as input to print Pascal's triangle.
(b) Inside the loop, start another loop from `k` equal to 0 up to `n - i - 1`, printing spaces.
(c) Within the inner loop, start another loop from `j` equal to 0 up to `i`.
(d) Inside the second inner loop, calculate `term` as `factorial(i) / (factorial(j) * factorial(i - j))`.
8. Stop.
Algorithm 27
1. Start
2. Declare an integer array 'a' of size 10 and variables 'n' and 'i'.
4. Initialize a loop from 0 to 'n-1' to input 'n' integers into array 'a'.
7. Stop
Algorithm 28
1. Start
2. Declare variables: n (for the size of the array), a[10] (array to store elements), pn (count of
positive numbers), nn (count of negative numbers), en (count of even numbers), on (count of odd
numbers), sop (sum of positive numbers), son (sum of negative numbers), soe (sum of even
numbers), soo (sum of odd numbers).
5. Initialize pn, nn, en, on, sop, son, soe, and soo to 0.
6. Loop through each element of the array using a loop variable i from 0 to n-1.
i. Increment pn by 1.
1. Increment en by 1.
i. Increment nn by 1.
1. Increment en by 1.
7. Print pn, nn, en, on, sop, son, soe, and soo.
8. Stop.
Algorithm 29
1. Start
5. Initialize `g` and `l` with the first element of array `a`.
6. Repeat the following steps for `i` from 0 to `n-1`:
11. Stop.
Algorithm 30
1. Start
- Read `matrix[i][j]`.
- Print `matrix[i][j]`.
18. Stop
Algorithm 31
1. Start
16. Stop.
- Start
- Declare function `read1` with parameters `int mat[10][10]`, `int r`, and `int c`.
- End loop
- End loop
- End
- Start
- Declare function `display` with parameters `int mat[10][10]`, `int r`, and `int c`.
- Print `mat[i][j]`.
- Print newline.
- End loop
- End loop
- End
- Start
- Set `mat3[i][j]` to 0.
- End loop
- End loop
- Print `mat3[i][j]`.
- Print newline.
- End loop
- End loop
- End
Algorithm 32
1. Start
4. Inside `readMatrix`:
- Print "Elements:"
6. Inside `printMatrix`:
- Print "Matrix:"
- Print "Transpose:"
9. In `main` function:
10. Stop.
Algorithm 33
1. Start
(a) If `arr[i]` is equal to 'key', print "found at index: i" and return 1.
9. Stop.
Algorithm 34
3. Prompt the user to enter a string and store it in the variable `str`.
4. Calculate the length of the string `n` using the `strlen` function.
- Check if the character at index `i` in the string is a vowel (a, e, i, o, u, A, E, I, O, U).
- If it is a vowel:
- Inside this loop, shift all characters one position left to overwrite the vowel.
7. Stop.
Algorithm 35
1. Start
3. Declare a function `palindrome(char pal[])` which takes a character array `pal` as input and returns
an integer.
(a) Get the length of the input string `pal` using `strlen()` and store it in variable `n`.
(b) Declare a character array `temp` of size 20 to store the reversed string.
(c) Use a for loop with index `i` ranging from 0 to `n-1`:
(d) Use `strcmp()` to compare the original string `pal` with the reversed string `temp`.
(c) Use `scanf()` to read the input string into the `str` array.
(d) Check if the return value of `palindrome(str)` is 0:
7. Stop.
Algorithm 36
1. Start
9. Initialize `sum` to 0.
13. Stop.
Algorithm 37
1. Start
8. Output "2. strlen(): " followed by the length of str1 using strlen().
14. Output "5. strncpy(): " followed by the result of strncpy(str3, str1, a).
18. Output "6. strncat(): " followed by the result of strncat(str1, str2, b).
19. Output "7. strcasecmp(): " followed by the result of strcasecmp(str1, str2).
23. Output "8. strncasecmp(): " followed by the result of strncasecmp(str1, str2, c).
27. Output "9. strspn(): " followed by the result of strspn(str1, d).
29. Output "10. strtok(): " followed by the result of strtok(str1, delim).
30. Stop
Algorithm 38
1. Start
2. Define a function named `stringLength` that takes a constant character pointer `str` as input.
- Set `length` to 0.
- Iterate through the characters of `str` until a null character ('\0') is encountered.
- Return `length`.
3. Define a function named `stringCopy` that copies the contents of one string to another.
- Accepts two character pointers `dest` and `src`.
8. Stop