Algorithms Collection
Algorithms Collection
Experiment No. 5
Problem Statement:
Write C Programs to Print Pyramids.
Algorithm:
Start
1. Input the number of rows (n)
2. For i = 1 to n
3. For j = 1 to n-i
4. Print space
5. For k = 1 to 2*i-1
6. Print "*"
7. Print newline
Stop
Experiment No. 6
Problem Statement:
WAP to accept the number of terms a finds the sum of sine series.
Algorithm:
Start
1. Input number of terms (n)
2. Initialize sum = 0
3. Initialize x (angle in radians)
4. For i = 0 to n-1
5. term = ((-1)^i * x^(2i+1)) / factorial(2i+1)
6. sum = sum + term
7. Print sum
Stop
Experiment No. 7
Problem Statement:
Write a menu driven program to perform following operations:
a.Perform addition of two 3X3 matrices.
Algorithm: b.Obtain transpose of a given 3X3 matrix.
c.Multiplication of two 3X3 matric
Start
1. Declare 3x3 matrices A, B, and Result
2. Display menu:
a. Addition
b. Transpose
c. Multiplication
3. Input choice
4. If choice is Addition:
5. Input matrices A and B
6. For i = 0 to 2
7. For j = 0 to 2
8. Result[i][j] = A[i][j] + B[i][j]
9. Display Result
1
10. If choice is Transpose:
11. Input matrix A
12. For i = 0 to 2
13. For j = 0 to 2
14. Result[j][i] = A[i][j]
15. Display Result
4. If choice is Length:
Without built-in:
5. Initialize count = 0
6. While str[count] != '\0'
7. Increment count
8. Print count
9. If choice is Reversal:
Without built-in:
10. Calculate length (len)
11. For i = 0 to len/2
12. temp = str[i]
13. str[i] = str[len-1-i]
14. str[len-1-i] = temp
15. Print reversed string
2
16. If choice is Equality:
Without built-in:
17. Input second string str2
18. Initialize flag = 1
19. For i = 0 until either string ends
20. If str[i] != str2[i]
21. flag = 0
22. break
23. Print result based on flag
fibonacci(n):
4. If n <= 1
5. Return n
6. Else
7. Return fibonacci(n-1) + fibonacci(n-2)
Stop
Experiment No. 10
Problem Statement:
WAP to search elements in an array using function
Algorithm:
Start
1. Input array size (n)
2. Input array elements
3. Input search element (key)
4. Call searchElement(array, n, key)
searchElement(array, n, key):
5. For i = 0 to n-1
6. If array[i] equals key
7. Return i
3
8. Return -1
Experiment No. 11
t Problem Statement:
WAP toaccept EMPLOYEE details (Name, Designation, Gender, DOJ and Salary). Define functionmembers tocompute
i) total number ofemployees inan organization
Algorithm: ii) Employee with salary more than 20,000
Start
1. Create Employee structure with fields:
- Name
- Designation
- Gender
- DOJ
- Salary
5. Function countEmployees():
6. Return total number of employees
7. Function highSalaryEmployees():
8. For i = 0 to n-1
9. If employee[i].salary > 20000
10. Print employee details
Experiment No. 12
Algorithm for Pointer Swap: Problem Statement:
i)To swap two numbers using pointers.
Start ii)Write a function to swap two integers using call by reference.
1. Input numbers a and b
2. Create pointers p1 = &a, p2 = &b
3. temp = *p1
4. *p1 = *p2
5. *p2 = temp
6. Print swapped numbers
Stop
4
Algorithm for Call by Reference:
Start
1. Input numbers a and b
2. Call swap(&a, &b)
swap(x, y):
3. temp = *x
4. *x = *y
5. *y = temp