0% found this document useful (0 votes)
14 views17 pages

Algorithms 17-38

Algorithms

Uploaded by

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

Algorithms 17-38

Algorithms

Uploaded by

magiccian67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Algorithm 17

1. Start

2. Read the integer value 'a'.

3. Read the power value 'b'.

4. Initialize a temporary variable 'temp' to 'a' and initialize variable 'i' to 1.

5. Repeat the following steps until 'temp' is greater than 10:

(a) Divide 'temp' by 10.

(b) Increment 'i' by 1.

6. Initialize 'sum' to 0 and reset 'temp' to 'a'.

7. Repeat the following steps 'i' times:

(a) Add the result of 'temp % 10' raised to the power of 'i' to 'sum'.

(b) Divide 'temp' by 10.

8. Check if 'sum' is equal to 'a':

(a) If true, print "Armstrong number".

(b) If false, print "Not an Armstrong number".

9. Initialize 'sum1' to 0 and reset 'temp' to 'a'.

10. Repeat the following steps 'i' times:

(a) Add the result of 'temp % 10' raised to the power of 'b' to 'sum1'.

(b) Divide 'temp' by 10.

11. Print the value of 'sum1'.

12. Stop.

Algorithm 18

1. Start

2. Declare variables `choice`, `num`, and `i`.

3. Label the repetition point as 'repeat'.

4. Print the options for the user:

- Print "Choose an option:"

- Print "1. Print squares up to n"

- Print "2. Exit"

- Print "Enter your choice: "

5. Read the user's choice into the variable `choice`.


6. Use a switch-case structure based on `choice`:

- Case 1:

- Print "n: "

- Read the value of `num`.

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

- Break out of the switch-case structure.

- Default:

- Print "Invalid choice\n".

- Go to 'repeat'.

7. Return 0.

8. Stop.

Algorithm 19

1. Start

2. Read the value of 'a' from the user.

3. Initialize a variable 'i' to 'a'.

4. Loop until 'i' is greater than or equal to 1.

a. Initialize a variable 'k' to 1.

b. Loop until 'k' is less than or equal to 'a' minus 'i'.

i. Print two spaces.

ii. Increment 'k' by 1.

c. Initialize a variable 'j' to 'i'.

d. Loop until 'j' is greater than or equal to 1.

i. Print the value of 'j' followed by a space.

ii. Decrement 'j' by 1.


e. Print a newline character.

f. Decrement 'i' by 1.

5. Stop

Algorithm 20

1. Start

2. Define function print():

(a) Print three dots followed by three asterisks and then three dots on separate lines.

3. Define function hello():

(a) Print "Hello! CodeTantra" followed by a newline character.

4. Define the main() function:

(a) Call print().

(b) Call hello().

(c) Call print().

5. Stop

Algorithm 21

1. Start

2. Declare a function `area_circle` which takes a single float parameter `radius`.

3. Inside the `area_circle` function:

(a) Declare a float variable `area`.

(b) Calculate `area` as `3.14 * radius * radius`.

(c) Print the value of `area`.

4. Declare the `main` function.

5. Inside the `main` function:

(a) Declare a float variable `radius`.

(b) Print "Enter radius : ".

(c) Read the value of `radius` using `scanf`.

(d) Call the function `area_circle` with `radius` as argument.

6. End `main` function.

7. End `area_circle` function.

8. Stop.

Algorithm 22
1. Start

2. Declare a function named `factorial` which takes an integer argument `number`.

3. Declare variables `i` and `fact`.

4. Set `fact` to 1.

5. Use a loop to iterate from `i = 1` to `number`.

- Inside the loop:

- Update `fact` as `fact * i`.

6. Return the value of `fact`.

7. End of the function.

8. In the `main` function:

- Declare a variable `number`.

- Print "Enter a number : ".

- Read the value of `number`.

- Print "Factorial of a given number %d = %d\n" with `number` and the result of calling the
`factorial` function with `number` as arguments.

9. End of the main function.

10. End.

Algorithm 23

1. Start

2. Define the function prime() with return type integer.

3. Inside the prime() function:

(a) Declare variables 'a' and 'sum'.

(b) Display "Enter a number: ".

(c) Read the input into variable 'a'.

(d) Initialize 'sum' to 0.

(e) Start a loop from 'i' equal to 1 to 'a'.

i. If 'a' modulo 'i' equals 0, then add 'i' to 'sum'.

(f) Check if 'sum' equals 'a' plus 1:

i. If true, return 0 (indicating a prime number).

ii. If false, return 84 (indicating not a prime number).


4. In the main function:

(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

2. Declare variables: `a`, `b`, `LCM`

3. Display "Enter two integer values: "

4. Read values of `a` and `b`

5. Set `LCM` to the result of calling `gcd(a, b)`

6. Display "The lcm of two numbers `a` and `b`: `LCM`"

7. Stop

Function gcd:

1. Start

2. Declare variables: `x`, `y`, `temp`, `lcm`

3. Set `x` to `a`, `y` to `b`

4. While `y` is not equal to 0, do steps 5-6:

a. Set `temp` to `y`

b. Set `y` to `x % y`

c. Set `x` to `temp`

5. Set `lcm` to `x`

6. Return `(a * b) / lcm`

7. Stop

Algorithm 25

1. Start

2. Include the necessary header file "declarations.h".

3. Define the main function:

(a) Declare an integer variable `num`.

(b) Prompt the user to input an integer.


(c) Read the input integer and store it in `num`.

(d) Calculate the factorial of `num` using the non-recursive function `factorial(num)` and store the
result in `fact`.

(e) Display the value of `fact`.

(f) Calculate the factorial of `num` using the recursive function `factorial_rec(num)` and store the
result in `fact_rec`.

(g) Display the value of `fact_rec`.

(h) Check if the number is prime using the function `prime(num)`.

(i) If the number is prime, display "Prime number"; otherwise, display "Not a prime number".

(j) Return 0 to indicate successful execution of the program.

4. Define the non-recursive function `factorial`:

(a) Initialize an integer variable `fac` to 1.

(b) Using a for loop, iterate from `n` down to 1:

(i) Multiply `fac` by the loop variable `i` and update `fac`.

(c) Return the value of `fac`.

5. Define the recursive function `factorial_rec`:

(a) Check if `n` is equal to 0 or 1.

(i) If true, return 1.

(b) If not, return `n` multiplied by the result of calling `factorial_rec(n-1)`.

6. Define the function `prime` to check if a number is prime:

(a) Initialize an integer variable `sum` to 0.

(b) Using a for loop, iterate from 1 to `n`:

(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

Sure, here's an algorithm for the provided C code:

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.

4. Inside the `factorial` function:


(a) Initialize `fac` to 1.

(b) Start a loop from `i` equal to `x` down to 1.

(c) Within the loop, calculate `fac = fac * i`.

(d) Return `fac`.

5. Define a function `pascal` that takes an integer `n` as input to print Pascal's triangle.

6. Inside the `pascal` function:

(a) Start a loop from `i` equal to 0 up to `n - 1`.

(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))`.

(e) Print `term`.

(f) After the inner loops, print a new line.

7. In the `main` function:

(a) Read the value of `n` from the user.

(b) Call the `pascal` function with `n` as input.

(c) Return 0 to indicate successful execution.

8. Stop.

Algorithm 27

1. Start

2. Declare an integer array 'a' of size 10 and variables 'n' and 'i'.

3. Read the value of 'n' from the user.

4. Initialize a loop from 0 to 'n-1' to input 'n' integers into array 'a'.

(a) Read an integer from the user and store it in 'a[i]'.

5. Print "Array: " to indicate the start of array printing.

6. Initialize a loop from 0 to 'n-1' to print the elements of array 'a'.

(a) Print 'a[i]' followed by a space.

7. Stop

Algorithm 28

Sure, here's an algorithm based on the given C code:

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

3. Read the value of n.

4. Read n elements into the array a[].

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.

a. Check if a[i] is greater than or equal to 0.

i. Increment pn by 1.

ii. Add a[i] to sop.

iii. Check if a[i] is even.

1. Increment en by 1.

2. Add a[i] to soe.

iv. Otherwise, increment on by 1 and add a[i] to soo.

b. If a[i] is less than 0:

i. Increment nn by 1.

ii. Add a[i] to son.

iii. Check if -a[i] is even.

1. Increment en by 1.

2. Add a[i] to soe.

iv. Otherwise, increment on by 1 and add a[i] to soo.

7. Print pn, nn, en, on, sop, son, soe, and soo.

8. Stop.

Algorithm 29

Sure, here's the algorithm for the C code you provided:

1. Start

2. Declare variables `n`, `a[10]`, `g`, and `l`.

3. Print "Enter the number of elements: ".

4. Read the value of `n`.

5. Initialize `g` and `l` with the first element of array `a`.
6. Repeat the following steps for `i` from 0 to `n-1`:

(a) Print "Enter a number: ".

(b) Read the value of `a[i]`.

7. Set `g` and `l` to `a[0]`.

8. Repeat the following steps for `i` from 0 to `n-1`:

(a) If `a[i]` is greater than `g`, set `g` to `a[i]`.

(b) If `a[i]` is less than `l`, set `l` to `a[i]`.

9. Print "Largest number: " followed by the value of `g`.

10. Print "Smallest number: " followed by the value of `l`.

11. Stop.

Algorithm 30

1. Start

2. Declare variables: `a[10][10]`, `b[10][10]`, `m`, `n`, `p`, `q`.

3. Define function `read1(matrix, rows, cols)`:

a. Initialize `i` and `j` to 0.

b. For each `i` from 0 to `rows - 1`, do:

i. For each `j` from 0 to `cols - 1`, do:

- Read `matrix[i][j]`.

4. Define function `display(matrix, rows, cols)`:

a. Initialize `i` and `j` to 0.

b. For each `i` from 0 to `rows - 1`, do:

i. For each `j` from 0 to `cols - 1`, do:

- Print `matrix[i][j]`.

- If `j` is not `cols - 1`, print a space.

ii. Print a new line.

5. Define function `additionOfTwoMatrices(matrix1, matrix2, rows, cols)`:

a. Initialize `i` and `j` to 0.

b. Declare a result matrix `result[10][10]`.

c. For each `i` from 0 to `rows - 1`, do:

i. For each `j` from 0 to `cols - 1`, do:

- Set `result[i][j]` to `matrix1[i][j] + matrix2[i][j]`.


d. Print "Resultant matrix after addition is:".

e. Call `display(result, rows, cols)` to display the result matrix.

6. Print "Enter the size of the first matrix : ".

7. Read integers `m` and `n`.

8. Call function `read1(a, m, n)` to read the first matrix.

9. Print "Enter the size of the second matrix : ".

10. Read integers `p` and `q`.

11. Call function `read1(b, p, q)` to read the second matrix.

12. Print "The first matrix is".

13. Call function `display(a, m, n)` to display the first matrix.

14. Print "The second matrix is".

15. Call function `display(b, p, q)` to display the second matrix.

16. If `m == p` and `n == q`, then:

a. Call function `additionOfTwoMatrices(a, b, m, n)` to add the matrices.

17. Else, print "Addition is not possible".

18. Stop

Algorithm 31

1. Start

2. Declare variables `a[10][10]`, `b[10][10]`, `m`, `n`, `p`, `q`.

3. Display message: "Enter the size of the first matrix : ".

4. Read values of `m` and `n`.

5. Call function `read1(a, m, n)`.

6. Display message: "Enter the size of the second matrix : ".

7. Read values of `p` and `q`.

8. Call function `read1(b, p, q)`.

9. Display message: "The first matrix is".

10. Call function `display(a, m, n)`.

11. Display message: "The second matrix is".

12. Call function `display(b, p, q)`.

13. Check if `n == p`.

14. If true, call function `multiplicationOfTwoMatrices(a, b, m, n, q)`.


15. Else, display message: "Multiplication is not possible".

16. Stop.

Algorithm for `read1` function:

- Start

- Declare function `read1` with parameters `int mat[10][10]`, `int r`, and `int c`.

- Display message: "Enter r*c elements : ".

- Loop `i` from 0 to `r-1`.

- Loop `j` from 0 to `c-1`.

- Read value `mat[i][j]`.

- End loop

- End loop

- End

Algorithm for `display` function:

- Start

- Declare function `display` with parameters `int mat[10][10]`, `int r`, and `int c`.

- Loop `i` from 0 to `r-1`.

- Loop `j` from 0 to `c-1`.

- Print `mat[i][j]`.

- Print newline.

- End loop

- End loop

- End

Algorithm for `multiplicationOfTwoMatrices` function:

- Start

- Declare function `multiplicationOfTwoMatrices` with parameters `int mat1[10][10]`, `int mat2[10]


[10]`, `int a`, `int b`, and `int c`.

- Declare `int mat3[10][10]`.

- Display message: "The resultant matrix is".


- Loop `i` from 0 to `a-1`.

- Loop `j` from 0 to `c-1`.

- Set `mat3[i][j]` to 0.

- Loop `k` from 0 to `b-1`.

- Update `mat3[i][j] += mat1[i][k] * mat2[k][j]`.

- End loop

- End loop

- Loop `i` from 0 to `a-1`.

- Loop `j` from 0 to `c-1`.

- Print `mat3[i][j]`.

- Print newline.

- End loop

- End loop

- End

Algorithm 32

1. Start

2. Initialize `rows` and `cols` to 5 (assuming these are default values).

3. Define function `readMatrix` with parameter `mat` representing a 2D array.

4. Inside `readMatrix`:

- Print "Elements:"

- Loop over rows from 0 to `rows-1`:

- Loop over columns from 0 to `cols-1`:

- Read an integer from input and store it in `mat[i][j]`.

5. Define function `printMatrix` with parameter `mat` representing a 2D array.

6. Inside `printMatrix`:

- Print "Matrix:"

- Loop over rows from 0 to `rows-1`:

- Loop over columns from 0 to `cols-1`:

- Print `mat[i][j]` followed by a space.

- Print a newline character.

7. Define function `transposeMatrix` with parameter `mat` representing a 2D array.


8. Inside `transposeMatrix`:

- Print "Transpose:"

- Loop over columns from 0 to `cols-1`:

- Loop over rows from 0 to `rows-1`:

- Print `mat[j][i]` followed by a space.

- Print a newline character.

9. In `main` function:

- Print "rows: ".

- Read an integer for `rows`.

- Print "columns: ".

- Read an integer for `cols`.

- Declare a 2D array `matrix` of size `rows x cols`.

- Call `readMatrix(matrix)` to read matrix elements.

- Call `printMatrix(matrix)` to print the original matrix.

- Call `transposeMatrix(matrix)` to print the transpose of the matrix.

10. Stop.

Algorithm 33

1. Start

2. Read the value 'n' for the size of the array.

3. Initialize an array `arr` of size 'n'.

4. Read 'n' numbers into the array `arr`.

5. Read the value 'key' to search for.

6. Initialize a variable `i` to 0.

7. Repeat the following steps till 'i' is less than 'n':

(a) If `arr[i]` is equal to 'key', print "found at index: i" and return 1.

(b) Increment 'i' by 1.

8. If the loop completes without finding 'key', print "No".

9. Stop.

Algorithm 34

Here's the algorithm based on the given C code:


1. Start

2. Declare a character array `str` of size 20.

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.

5. Initialize a loop with variable `i` from 0 to `n-1`.

- Inside the loop:

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

- Start another loop with variable `j` from `i` to `n-1`.

- Inside this loop, shift all characters one position left to overwrite the vowel.

- Decrement `i` by 1 to handle consecutive vowels.

6. Print the modified string without vowels.

7. Stop.

Algorithm 35

Here's an algorithm for the provided C code:

1. Start

2. Include necessary header files like `<stdio.h>` and `<string.h>`.

3. Declare a function `palindrome(char pal[])` which takes a character array `pal` as input and returns
an integer.

4. Inside the `palindrome` function:

(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`:

(i) Assign `pal[i]` to `temp[n-i-1]`.

(d) Use `strcmp()` to compare the original string `pal` with the reversed string `temp`.

(e) Return the result of the comparison.

5. In the `main()` function:

(a) Declare a character array `str` of size 20.

(b) Print a message prompting the user to enter a string.

(c) Use `scanf()` to read the input string into the `str` array.
(d) Check if the return value of `palindrome(str)` is 0:

(i) If it is 0, print "palindrome".

(ii) If not, print "not a palindrome".

6. Return 0 to indicate successful execution.

7. Stop.

Algorithm 36

1. Start

2. Declare variables `num1_str[100]`, `num2_str[100]`, `m`, `n`, and `sum`.

3. Print "num1: ".

4. Read input into `num1_str`.

5. Print "num2: ".

6. Read input into `num2_str`.

7. Initialize `m` as the length of `num1_str`.

8. Initialize `n` as the length of `num2_str`.

9. Initialize `sum` to 0.

10. Iterate over each character `i` in `num1_str` from 0 to `m - 1`:

- Calculate `sum += (num1_str[i] - '0') * pow(10, m - i - 1)`.

11. Iterate over each character `i` in `num2_str` from 0 to `n - 1`:

- Calculate `sum += (num2_str[i] - '0') * pow(10, n - i - 1)`.

12. Print "Sum: " followed by the value of `sum`.

13. Stop.

Algorithm 37

1. Start

2. Declare character arrays str1[50], str2[50], and str3[50].

3. Output "string1: ".

4. Input a string into str1 using scanf().

5. Output "string2: ".

6. Input a string into str2 using scanf().

7. Output "1. strcat(): " followed by the result of strcat(str1, str2).

8. Output "2. strlen(): " followed by the length of str1 using strlen().

9. Output "3. strcpy(): " followed by str1.


10. Output "4. strcmp(): " followed by the result of strcmp(str1, str2).

11. Declare an integer variable a.

12. Output "characters to be copied: ".

13. Input an integer into a using scanf().

14. Output "5. strncpy(): " followed by the result of strncpy(str3, str1, a).

15. Declare an integer variable b.

16. Output "characters to be concatenated: ".

17. Input an integer into b using scanf().

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

20. Declare an integer variable c.

21. Output "characters to be compared: ".

22. Input an integer into c using scanf().

23. Output "8. strncasecmp(): " followed by the result of strncasecmp(str1, str2, c).

24. Declare a character array d[10].

25. Output "Enter initial segment: ".

26. Input a string into d using scanf().

27. Output "9. strspn(): " followed by the result of strspn(str1, d).

28. Declare a character array delim[] containing the delimiter ','.

29. Output "10. strtok(): " followed by the result of strtok(str1, delim).

30. Stop

Algorithm 38

Here's the algorithm corresponding to the C code provided:

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.

- Increment `length` for each character 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`.

- Iterate through `src` until a null character is encountered.

- Copy each character of `src` to `dest`.

4. Define a function named `stringReverse` that reverses the contents of a string.

- Accepts a character pointer `str`.

- Obtain the length of `str` using `stringLength`.

- Create a temporary character array `temp` of the same length.

- Iterate through the first half of `str`.

- Swap characters from the beginning and end of `str`.

5. Define a function named `stringCompare` that compares two strings.

- Accepts two constant character pointers `str1` and `str2`.

- If the lengths of `str1` and `str2` are different, return -1.

- Iterate through both strings simultaneously and compare characters.

- If any character differs, return -1.

- If both strings are identical, return 0.

6. Define a function named `stringConcatenate` that concatenates two strings.

- Accepts two character pointers `dest` and `src`.

- Append the characters of `src` to the end of `dest`.

7. In the `main` function:

- Declare character arrays `str1`, `str2`, and `result`.

- Input a string into `str1` and `str2` using `scanf`.

- Obtain the length of `str1`.

- Copy the contents of `str1` to `result`.

- Reverse the contents of `str2`.

- Compare `str1` and `str2`.

- Copy `str1` to `result`, reverse `str2`, and concatenate it with `result`.

- Print the result.

8. Stop

You might also like