A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C program to calculate power of a number using recursion
Q:

C program to calculate power of a number using recursion

0

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now