ADVERTISEMENT
ADVERTISEMENT

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.

Calculate power of a number program 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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.