Search C Program | nimishsoft@gmail.com

Power by Recursion

/* Power by Recursion */

#include<stdio.h>
int pow(int,int);

int main()
{
 int i,j;
 printf("Type two values : \n");
 scanf("%d %d",&i,&j);
 
 printf("i pow j = %d",pow(i,j));
 return 0;
}

int pow(int i,int j)
{
 if(j==1)
  return i;
 return (i*pow(i,j-1));
}

4 comments:

  1. What if exponent is 0???

    ReplyDelete
    Replies
    1. there should b another base condition:
      if ( j = = 0 )
      return 1;

      Delete
  2. rec5.c:4:5: warning: conflicting types for built-in function 'pow'
    int pow(int,int);

    ReplyDelete