FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM





Find power of a number using recursion using c program


#include<stdio.h>
int main(){
  int pow,num;
  long int res;
  long int power(int,int);
  printf("\nEnter a number: ");
  scanf("%d",&num);
  printf("\nEnter power: ");
  scanf("%d",&pow);
  res=power(num,pow);
  printf("\n%d to the power %d is: %ld",num,pow,res);
  return 0;
}
  int i=1;
  long int sum=1;
  long int power(int num,int pow){
      if(i<=pow){
           sum=sum*num;
          power(num,pow-1);
      }
      else
      return sum;
  }




Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

16 comments:

Anonymous said...

thank u very much

Anonymous said...

it does not works for power(10,6)...why?

Anonymous said...

it's working for (10,6) also once check it....

reny said...

can you help me with this one? program that will print out

powers of 2: 1, 2, 4, 8, .. up to 2n, where n is a user input.

yogesh chandel said...

It Does n't work

Anonymous said...

this power function is not efficient...
modify like this>>>>>>>
A better recursion function for power calculation...
pow(int a,int b)
{
if(b==0)
return 1;
if(b%2==0)
return pow(a*a,b/2);
return pow(a*a,b/2)*a;
}

Anonymous said...

good job

Doctor N said...

Yr what is this? It is not working. Make it right, otherwise my girlfriend will kill me. do it fast. Tomorrow will b her practicals, I am management student. Did not know what the hell are you doing with this C

Anonymous said...

try this ....this code surely works :) chancahl tripathi:)
#include
#include
int power(int x,int p);
void main()
{
int n,p;
clrscr();
printf("enter no.and power");
scanf("%d%d",&n,&p);
printf("%d",power(n,p));
getch();
}
int power(int x,int p)
{
if(p==0)
return 1;
else
return (x*power(x,p-1));

}

Pawan Sen said...

awesome.

Anonymous said...

#include
#include
#include
int power(int,int,int);
main()
{
int num,i=1,p,result;
printf("Enter a number:");
scanf("%d",&num);
printf("Enter power:");
scanf("%d",&p);
result=power(num,i,p);
printf("\n%d to the power of %d is: %d",num,p,result);
getch();
}
int power(int num,int i,int p)
{
if(i==p)
{
return pow(num,i);
}
else
{
return power(num,i+1,p);
}
}


omale said...

#include
#include
unsigned int Number_a_to_number_b(unsigned int a, unsigned int b);
unsigned int x,y;
unsigned int answer=0;
int main()
{
printf("Enter a base");
scanf("%u",&x );
printf("Enter a power");
scanf("%u",&y);
answer = Number_a_to_number_b(x,y);
printf("%u to power %u is %u",x,y,answer);

return 0;
}
unsigned int Number_a_to_number_b(unsigned int a, unsigned int b)
{
if (b==0)
return 1;
if (b==1)
return a;
//answer*=a;

return a*Number_a_to_number_b(a,(b-1));

}

Unknown said...

(i=1;i<=Thenumberyouinput;i++);
Answer=i*2

try it

Unknown said...

this program much better this
#include
int power(int a,int b);
main()
{
int a,out;
int b;
printf("Enter Power :");
scanf("%d",&a);
printf("Enter base");
scanf("%d",&b);
out= power(a,b);
printf("power=%d",out);
getch();
}
int power(int x, int y)
{
int i,t=1;
for(i=1;i<=y;i++)
{
t=t*x;
}
return t;
}

ناصرخان صياد said...

(Easy way for power)


#include
void main()
{
int base,index,pow=1;
printf("\n Enter the Base : ");
scanf("%d",&base);
printf("\n Enter the Index : ");
scanf("%d",&index);
for(int i=1;i<=y;i++)
{
pow=pow*base;
}
printf("\n %d^%d = %d",base,index,pow);
}

ناصرخان صياد said...

(Easy way fo power)


#include
void main()
{
int base,index,pow=1;
printf(" Enter the Base : ");
scanf("%d",&base);
printf(" Enter the Index : ");
scanf("%d",&index);
for(int i=1;i<=index;i++)
{
pow=pow*base;
}
printf("%d^%d=%d",base,index,pow);
}