-->

Facebook

Jasper Roberts - Blog

Monday, December 19, 2016

Write a C Program to Find power of a number using recursion.

  1. #include<stdio.h>
  2. #include<conio.h>

  3. int main() {
  4. int pow,no;
  5. long int res;
  6. long int power(int,int);
  7. printf("Enter a number: ");
  8. scanf("%d",&no);
  9. printf("\n Enter power: ");
  10. scanf("%d",&pow);
  11. res=power(no,pow);
  12. printf("\n%d to the power %d is: %ld",no,pow,res);
  13. return 0;
  14. }
  15. int i=1;
  16. long int sum=1;
  17. long int power(int num,int pow) {
  18. if(i<=pow) {
  19. sum=sum*no;
  20. power(no,pow-1);
  21. } else
  22. return sum;
  23. }
OUTPUT:
Enter a number: 5
Enter power: 2
5 to the power 2 is: 25