You are on page 1of 4

Experiment - 2

Student Name: Kartik Guleria UID: 19BCS2241


Branch: CSE Section/Group: CSE-9/A
Semester: 5th Date of Performance: 29/8/2021
Subject Name: Design & analysis of algorithm Subject Code:CSP-309

1. Aim/Overview of the practical: Code to implement power function in O(log n) time


complexity.

2. Task to be done/ Which logistics used:

We learn how to get the power of a number.


3. Algorithm/Flowchart (For programming based labs):
Step 1: Let a, b be two numbers

Step 2: create a power function

Step 3: if y %2 equals to 0 then temp*temp

Step 4: if not then the number * temp square

Step 5: print the number

Step 6: Finish
4. Steps for experiment/practical/Code:
#include <iostream>
using namespace std;
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
int main(){
int num;
int a = 4;
int b = 3;
num = power(a,b);
cout<<"KARTIK GULERIA\n";
cout<<"19BCS2241\n";
cout<<a<<" raise to the Power "<<b<<" is "<<num;
}

5. Observations/Discussions/ Complexity Analysis:

6. Result/Output/Writing Summary:

4 raise to the power 3 is 64.


Learning outcomes (What I have learnt):

1.We have learnt to calculate the power of a number.

2.We have learnt about time complexity.


Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like