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

Algorithms Collection

Uploaded by

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

Algorithms Collection

Uploaded by

Siddhant patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

16. If choice is Multiplication:


17. Input matrices A and B
18. For i = 0 to 2
19. For j = 0 to 2
20. Result[i][j] = 0
21. For k = 0 to 2
22. Result[i][j] += A[i][k] * B[k][j]
23. Display Result
Stop
Experiment No. 8
Problem Statement:
WAP to accepts a string from user and perform string operations using inbuilt string
function and without using string functions-
i) length of string
Algorithm: ii) String reversal
iii) Equality check of two strings
Start iv) Check palindrome
1. Input string str
2. Display menu:
a. Length
b. Reversal
c. Equality check
d. Palindrome check
3. Input choice

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

24. If choice is Palindrome:


25. Create reversed string using reversal algorithm
26. Check equality with original string
27. Print result
Stop
Experiment No. 9
Problem Statement:
WAP to accept from user the number of Fibonacci numbers to be generated
and print the Fibonacci series using recursion
Algorithm:
Start
1. Input number of terms (n)
2. For i = 0 to n-1
3. Call fibonacci(i) and print result

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

9. Print search result


Stop

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

2. Input number of employees (n)


3. For i = 1 to n
4. Input employee details

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

11. Display results


Stop

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

6. Print swapped numbers


Stop

You might also like