Lab 4 Sol
Lab 4 Sol
Submitted to:
Haroon Ibrahim.
Submitted By:
Muhammad Arslan
Haider.
EE-20A.
210401027.
Lab Task #01
Task No 1:
Write a function that takes two arguments as an input and
recursively computes the power of the first number to the
second number.
Algorithm:
Start.
Declaring variable and using float .
Entering the value of variable.
Using else statement with the power function.
Now using second function for input output.
Take output by entering the values of “N”.
Enter the power of “N”.
Take value of power of input number.
Print out the result.
Close the program.
Code:
#include<iostream>
using namespace std;
int exp(float n, int pow){
if(pow==1)
return n;
else{
pow = pow-1;
n=n*exp(n,pow); //25*25
return n;
}
}
int main(){
int pow;
float num;
cout<<"\nvalue of N ";
cin>>num;
cout<<"\nPower of N ";
cin>>pow;
}
Screen Shot:
Repl.it Link:
https://replit.com/@ArslanAsh/Lab-4-task-1#main.cpp
Lab Task #2
Task No 2:
Write a program and recurrence relation to find the Fibonacci
series of 𝒏 where 𝒏 > 𝟐. Note: In mathematics, the Fibonacci
numbers, form a sequence, called the Fibonacci sequence, such
that each number is the sum of the two preceding ones,
starting from 0 and 1. That is, 𝐹0 = 0, 𝐹1 = 1 and 𝐹𝑛 = 𝐹𝑛−1 +
𝐹𝑛−2 for 𝑛 > 1. The beginning of the sequence is thus 0, 1, 1, 2,
3, 5, 8, 13, 21, 34, 55, 89, 144, …
Algorithm:
Start.
Define integers globally.
Prototype of Function type_name_(data type_variable)
Main function.
While loop from a=0 to a<f;
Print function;
Loop end;
Function type_name_(data type_variable)
if f=0 and f=1,then
return f;
else
return fib(f-1)+fib(f-2);
if end;
End
Code:
#include <iostream>
using namespace std;
int fib(int n);
int a=0,f;
int main()
{
cout<<"Enter a number f to print the Fibonacci series of f
number ";
cin>>f;
cout<<"Fibonacci series of "<<f<<" number is ";
while (a<f)
{
cout<<fib(a)<<" ";
a++;
}
return 0;
}
int fib(int f)
{
if ((f==0)||(f==1))
{
return f;
}
else
return (fib(f-1)+fib(f-2));
}
Screen Shot:
Repl.it Link:
https://replit.com/@ArslanAsh/Lab-4-task-2#main.cpp