IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
Practical 1
Aim:
Implement and analyze the algorithms given below.
Program: Factorial Iterative
#include <iostream>
using namespace std;
int main()
{
int number, fact = 1, count = 0;
count++;
cout << "Enter a Number:- ";
cin >> number;
count++;
for (int i = 1; i <= number; i++)
{
count++;
fact = fact * i;
count++;
count++;
}
count++;
cout << fact;
cout << endl;
cout << "count:" << count;
return 0;
}
Output:
ID No: - D22IT184 Page 1
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
Analysis Table:
INPUT COUNTER
5 18
10 33
15 48
20 63
25 78
Graph:
ID No: - D22IT184 Page 2
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
Program: FACTORIAL Recursive
#include <iostream>
using namespace std;
int count = 0;
int find_factorial(int n)
{
count++;
count++;
if (n < 0)
{
count++;
return (-1);
}
count++;
if (n == 0)
{
count++;
return (1);
}
else
{
count++;
count++;
return (n * find_factorial(n - 1));
}
}
int main()
{
int number, result;
cout << "Enter a Number:- ";
cin >> number;
result = find_factorial(number);
cout << result;
cout << endl;
cout << count;
return 0;
}
ID No: - D22IT184 Page 3
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
Output:
Analysis Table:
INPUT COUNTER
5 29
10 54
15 79
20 104
25 129
Graph:
ID No: - D22IT184 Page 4
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
Program: FIBONACCI Iterative
#include <iostream>
using namespace std;
int main()
{
int count = 0;
int i, number, n1 = 0, n2 = 1, n3;
count++;
count++;
cout << "Enter a number:- ";
cin >> number;
cout << n1 << " " << n2 << " ";
count++;
for (i = 2; i < number; i++)
{
count++;
n3 = n1 + n2;
count++;
cout << n3 << " ";
n1 = n2;
count++;
n2 = n3;
count++;
count++;
}
count++;
cout << endl;
cout << "count:" << count;
return 0;
}
Output:
Analysis Table:
ID No: - D22IT184 Page 5
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
INPUT COUNTER
5 19
10 44
15 69
20 94
25 119
Graph:
Program: FIBONACCI Recursive
ID No: - D22IT184 Page 6
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
#include <iostream>
using namespace std;
int c=0;
int fib(int x) {
c++;
c++;
if((x==1)||(x==0)) {
c++;
return(x);
c++;
}else {
c++;
return(fib(x-1)+fib(x-2));
}
}
int main() {
c++;
int x , i=0;
cout << "Enter the number of terms of series:- ";
cin >> x;
cout << "\nFibonnaci Series : ";
while(i < x) {
c++;
cout << " " << fib(i);
i++;
}
c++;
cout<<"\nNumber of counter : "<<c;
return 0;
}
Output:
Analysis Table:
ID No: - D22IT184 Page 7
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
INPUT COUNTER
5 64
10 840
15 9548
20 106222
25 1178454
Graph:
Program: Linear Search Iterative
ID No: - D22IT184 Page 8
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
#include<iostream>
using namespace std;
int counter;
void linearSearch(int arr[], int n,int k){
bool flag = true;
counter++;
for(int i=0; i<n; i++){
counter++;
counter++;
if(arr[i]==k){
cout << k << " is found " << endl;
flag = false;
break;
}
counter++;
}
counter++;
counter++;
if(flag){
cout << k << " not found" << endl;
}
cout << "Value of counter is :- " << counter << endl;
int main(){
int n, k;
cout << "Enter the size of array :- ";
cin>>n;
int* arr = new int[n];
cout << "Enter the array elements :- ";
counter++;
for(int i=0; i<n; i++){
counter++;
cin>>arr[i];
ID No: - D22IT184 Page 9
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
counter++;
}
counter++;
cout << "Enter the Key :- ";
cin>>k;
counter++;
linearSearch(arr,n,k);
counter++;
return 0;
}
Output:
Analysis Table:
Best Case Average Case Worst Case
Input counter counter counter
5 18 24 31
10 28 40 55
15 38 56 80
20 48 75 106
25 58 94 131
ID No: - D22IT184 Page 10
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
Graph:
Linear Search Iterative
140
120
100
80
Cases
60
40
20
0
0 5 10 15 20 25 30
Inputs
Best Case counter Linear (Best Case counter)
Average Case counter Linear (Average Case counter)
Worst Case counter Linear (Worst Case counter)
Program: Linear Search Recursive
ID No: - D22IT184 Page 11
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
#include <iostream>
using namespace std;
int counter;
int linearSearchrec(int arr[], int n, int k)
{
counter++;
if (n == 0)
{
counter++;
return -1;
}
counter++;
if (arr[n - 1] == k)
{
counter++;
return n - 1;
}
counter++;
return linearSearchrec(arr, n - 1, k);
}
int main()
{
int n, k;
cout << "Enter the value of n :- ";
cin >> n;
int *arr = new int[n];
cout << "Enter the array elements :- ";
counter++;
for (int i = 0; i < n; i++)
{
counter++;
cin>>arr[i];
counter++;
}
counter++;
ID No: - D22IT184 Page 12
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
cout << "Enter the value of key :- ";
cin >> k;
counter++;
int ans = linearSearchrec(arr, n, k);
counter++;
if (ans)
{
cout << k << " is found " << endl;
}
cout << "Value of Counter is :- " << counter << endl;
counter++;
return 0;
}
Output:
Analysis Table:
Best Case Average Case Worst Case
Input counter counter counter
5 29 24 31
10 28 40 55
15 38 56 80
20 48 75 106
25 58 94 131
ID No: - D22IT184 Page 13
IT341– Design & Analysis of Algorithm 5-IT-2 (2023-2024)
Graph:
Linear Search recursive
140
120
100
80
60
40
20
0
0 5 10 15 20 25 30
Best Case counter Linear (Best Case counter)
Average Case counter Linear (Average Case counter)
Worst Case counter Linear (Worst Case counter)
Conclusion:
In this Practical We have done Analysis of
Fibonacci Series (Iterative and Recursive) and
Factorial (Iterative and Recursive) and
Linear Search (Iterative and Recursive) and
kept the counter and Counted the Number of instructions Executed and on the basis of analysis we
created a graph and get linear equation in case of iterative method.
ID No: - D22IT184 Page 14