Skip to content

Commit 4a71ab7

Browse files
authored
Merge pull request #420 from sejal1126/patch-1
Create Power of n element
2 parents fdbb728 + dbd9eb5 commit 4a71ab7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Divide and Conquer/Powerofnumber.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
#include <iostream>
3+
4+
using namespace std;
5+
int powerofnumber(int a,int n)
6+
{int mid=0,c=0,b=0;
7+
if(n==1)
8+
{
9+
return a;
10+
}
11+
else
12+
{
13+
mid=n/2;
14+
b=powerofnumber(a,mid);
15+
c=b*b;
16+
if(n%2==0)
17+
{
18+
return c;
19+
}
20+
else
21+
{
22+
return a*c;
23+
}
24+
}
25+
}
26+
int main()
27+
{ int num=0,ans=0,power=0;
28+
cout<<"Power of n element\n";
29+
cout<<"\nEnter the Number\t=\t";cin>>num;
30+
cout<<"Enter the power\t=\t";cin>>power;
31+
ans= powerofnumber(num,power);
32+
cout<<"Answer\t=\t"<<ans;
33+
return 0;
34+
}

0 commit comments

Comments
 (0)