D22IT184 DAA Practical1
D22IT184 DAA Practical1
Practical 1
Aim:
Implement and analyze the algorithms given below.
#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:
Analysis Table:
INPUT COUNTER
5 18
10 33
15 48
20 63
25 78
Graph:
#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;
}
Output:
Analysis Table:
INPUT COUNTER
5 29
10 54
15 79
20 104
25 129
Graph:
#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:
INPUT COUNTER
5 19
10 44
15 69
20 94
25 119
Graph:
#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:
INPUT COUNTER
5 64
10 840
15 9548
20 106222
25 1178454
Graph:
#include<iostream>
using namespace std;
int counter;
counter++;
if(flag){
cout << k << " not found" << endl;
}
int main(){
int n, k;
counter++;
for(int i=0; i<n; i++){
counter++;
cin>>arr[i];
counter++;
}
counter++;
counter++;
linearSearch(arr,n,k);
counter++;
return 0;
}
Output:
Analysis Table:
5 18 24 31
10 28 40 55
15 38 56 80
20 48 75 106
25 58 94 131
Graph:
60
40
20
0
0 5 10 15 20 25 30
Inputs
#include <iostream>
using namespace std;
int counter;
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;
counter++;
}
counter++;
counter++;
int ans = linearSearchrec(arr, n, k);
counter++;
if (ans)
{
cout << k << " is found " << endl;
}
counter++;
return 0;
}
Output:
Analysis Table:
5 29 24 31
10 28 40 55
15 38 56 80
20 48 75 106
25 58 94 131
Graph:
Conclusion: