×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to calculate power of a number using recursion

In this program we will read base and power and then calculate result of that expression using recursion.

C program to calculate power of a number using recursion

/*C program to calculate power of any number using recursion*/

#include <stdio.h>

//function for calculating power
long int getPower(int b, int p)
{
    long int result = 1;
    if (p == 0)
        return result;
    result = b * (getPower(b, p - 1)); //call function again
}
int main()
{
    int base, power;
    long int result;

    printf("Enter value of base: ");
    scanf("%d", &base);

    printf("Enter value of power: ");
    scanf("%d", &power);

    result = getPower(base, power);

    printf("%d to the power of %d is: %ld\n", base, power, result);

    return 0;
}

Output

Enter value of base: 10
Enter value of power: 4
10 to the power of 4 is: 10000

C Recursion Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.