Tuesday, 4 February 2014

program, using recursion, finds the power of a number.

/*
 * C Program to find Power of a Number using Recursion
 */if u like please share my blog
#include <stdio.h>

long power (int, int);

int main()
{
    int pow, num;
    long result;

    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Enter it's power: ");
    scanf("%d", &pow);
    result = power(num, pow);
    printf("%d^%d is %ld", num, pow, result);
    return 0;
}

long power (int num, int pow)
{
    if (pow)
    {
        return (num * power(num, pow - 1));
    }
    return 1;
}




2nd code

#include<stdio.h>
 #include<conio.h>
  void main()
  {
      int x,y,p; printf("\nletsdoprogramminc.blogspot.com");
       printf("\nEnter X and Y:")
       scanf("%d%d",&x,&y);
        p=power(x,y);
         printf("Power of X rest to Y=%d",p);

           }
           int power(int a,int b)
    {
int c=1,i=1; while(i<=b)
    {
    c=c*a; i++;
                     }
                      return c;
                      }

No comments:

Post a Comment