Professional Documents
Culture Documents
1.1 FIBONACCI
1.1.1 FibonaCCi iterative
Code
#include<iostream>
int counter=0;
int fibonacci(int n)
{
int f0 = 0,f1 = 1,f;
if(n==0)
{
return 0;
}
if(n==1)
{
return 1;
}
else
{
for(int i=2;i<=n;i++)
{
f = f0 +f1;
f0 = f1;
f1 = f;
counter+=3;
}
return f;
}
}
int main()
{
int n;
cout<<"enter the number of element :";
cin >> n;
cout<<"fibonaki : "<<fibonacci(n)<<endl;
cout<<"total no. of count :"<<counter<<endl;
}
CE355 Practical -1 ID: 20CE120
Output
#include <iostream>
int counter=0;
int fibonacci(int n)
{
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
CE355 Practical -1 ID: 20CE120
counter++;
return fibonacci(n-1) + fibonacci(n-2);
}
}
int main() {
// Write C++ code here
int n;
cout<<"enter the no. of elements :";
cin >> n;
cout<<"fibonacci : "<<fibonacci(n)<<endl;
cout<<"total no. of count :"<<counter;
return 0;
}
output
CE355 Practical -1 ID: 20CE120
n Cnt _for_iterative
3 6
5 12
10 27
12 33
15 42
Chart
cnt_for_interation
45
y = 3x - 3
40
35
30
25
20
15
10
5
0
0 2 4 6 8 10 12 14 16
n cnt_for_recursive
3 2
4 4
5 7
6 12
7 20
CE355 Practical -1 ID: 20CE120
chart
cnt_for_recursion
25
y = 0.3865e0.5704x
20
15
10
0
0 1 2 3 4 5 6 7 8
CE355 Practical -1 ID: 20CE120
1.2 FACTORIAL
1.2.1 Factorial iterative
Code
#include <iostream>
int count=0;
int ans =1;
int fac(int n)
{
if(n==0)
{
return ans;
}
else if(n==1)
{
return ans;
}
else
{
for(int i=2;i<=n;i++)
{
count++;
ans*=i;
}
return ans;
}
}
int main() {
// Write C++ code here
int n;
CE355 Practical -1 ID: 20CE120
return 0;
}
Output
#include <iostream>
int count=0;
int ans =1;
CE355 Practical -1 ID: 20CE120
int fac(int n)
{
if(n==0)
{
return ans;
}
else if(n==1)
{
return ans;
}
else
{
count++;
return n*fac(n-1);
}
}
int main() {
// Write C++ code here
int n;
cout<<"enter the number of elements :";
cin >> n;
cout<<"factorial :"<<fac(n)<<endl;
cout<<"total no. of count :"<<count;
return 0;
}
Output
CE355 Practical -1 ID: 20CE120
CE355 Practical -1 ID: 20CE120
n Cnt_for_iterative
3 2
4 3
5 4
6 5
7 6
Chart
cnt_for_iterative
7
y=x-1
6
0
0 1 2 3 4 5 6 7 8
n Cnt_for_recursive
3 2
4 3
5 4
6 5
CE355 Practical -1 ID: 20CE120
7 6
Chart
cnt_for_recursive
7
6 y=x-1
0
0 1 2 3 4 5 6 7 8
CE355 Practical -1 ID: 20CE120
if(arr[i] == Search)
{
flag++;
break;
}
counter+=3;
}
counter+=1;
if(flag!=0)
{
cout<< "Item found"<<endl;
}
else
{
cout<< "Item not found"<<endl;
}
cout << "no. of counter = " << counter<<endl;
}
int main()
{
CE355 Practical -1 ID: 20CE120
int n,A;
cout << "enter size of array :"<<endl;
cin >> n;
int Array[n];
cout<<"enter the elements :"<<endl;
for(int i=0;i<n;i++)
{
cin>> Array[i];
}
cout<< "enter the search the elements :"<<endl;
cin >> A;
linear(Array,n,A);
return 0;
}
Output Best_case
CE355 Practical -1 ID: 20CE120
Average_case
CE355 Practical -1 ID: 20CE120
Worst_ case
CE355 Practical -1 ID: 20CE120
CE355 Practical -1 ID: 20CE120
Chart
count_of_best_case
1.2
1 y=1
0.8
0.6
0.4
0.2
0
0 2 4 6 8 10 12 14 16
Average_case
Table
CE355 Practical -1 ID: 20CE120
Chart count_of_average_case
25
y = 1.4383x + 0.0309
20
15
10
0
0 2 4 6 8 10 12 14 16
Worst_case
Table
Chart
CE355 Practical -1 ID: 20CE120
count_of_worst_case
50
45 y = 3x + 1
40
35
30
25
20
15
10
5
0
0 2 4 6 8 10 12 14 16
CE355 Practical -1 ID: 20CE120
int counter=0;
int binary(int arr[],int key,int low,int high)
{
int mid;
counter++;
if(low>high) return -1;
mid = low+high/2;
counter++;
if(key == arr[mid])
{
return mid;
}
else if(key<arr[mid])
counter++;
return binary(arr,key,low,mid-1);
counter++;
binary(arr,key,mid+1,high);
}
int main()
{
int n,A;
cout << "enter size of array :"<<endl;
cin >> n;
int Array[n];
cout<<"enter the elements :"<<endl;
for(int i=0;i<n;i++)
CE355 Practical -1 ID: 20CE120
{
cin>> Array[i];
}
cout<< "enter the search the elements :"<<endl;
cin >> A;
int position = -1;
int Low = 0;
int High = n-1;
position = binary(Array,A,Low,High);
if(position!=-1)
{
cout<<"number present at index :"<<position+1<<endl;
}
else{
cout<<"the number is not present in the list"<<endl;
}
cout<<"total no. of count :"<< counter<<endl;
return 0;
}
Output Best_case
CE355 Practical -1 ID: 20CE120
Average_case
CE355 Practical -1 ID: 20CE120
Worst_case
CE355 Practical -1 ID: 20CE120
CE355 Practical -1 ID: 20CE120
Chart
count_of_best_case
2.5
2 y=2
1.5
0.5
0
0 2 4 6 8 10 12 14 16
Average_case
Table
n Cnt_of_average_case
3 5
CE355 Practical -1 ID: 20CE120
5 6
8 5
12 7
15 11
Chart
count_of_average_case
12
10
y = 0.428x + 3.1193
0
0 2 4 6 8 10 12 14 16
Worst_case
Table
n Cnt_of_worst_case
3 5
5 6
8 7
12 9
15 11
Chart
CE355 Practical -1 ID: 20CE120
count_of_worst_case
12
y = 0.4856x + 3.4239
10
0
0 2 4 6 8 10 12 14 16