To find power of a number using recursion
Question:
Write a program to find the power of a number using recursion.
Logic:
- Get the base value and also power value from the user.
- Then the base number multiplies with itself for power value.
- And return the calculated value.
Algorithm:
- Get the base value as m and also power value as n.
- Then m multiplies with itself for n times as recursively.
- Return the calculated value the n reaches 0.
Program:
#include<stdio.h> void main() { int m,n; clrscr(); printf("enter the base no:"); scanf("%d",&m); printf("enter the power no:"); scanf("%d",&n); /*the value m and n passes as an argument into the function*/ printf("the value of number is: %d",pow(m,n)); getch(); } int pow(int m,int n) { if(n!=0) { return m*pow(m,n-1); //the value m mutiplies itself of n times then return value } else { return 1; //returns when value is zero } }