C Program to Calculate the Power using Recursion

This is a C Program to find power of a number using recursion.

Problem Description

The following C program, using recursion, finds the power of a number.

Problem Solution

The power of a number is the number multiplied to itself for the number of times it has been raised to Eg: 73 is 343

Program/Source Code

Here is the source code of the C program to find an element in a linked list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to find Power of a Number using Recursion
 */
#include <stdio.h>
 
long power (int, int);
 
int main()
{
    int pow, num;
    long result;
 
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Enter it's power: ");
    scanf("%d", &pow);
    result = power(num, pow);
    printf("%d^%d is %ld", num, pow, result);
    return 0;
}
 
long power (int num, int pow)
{
    if (pow)
    {
        return (num * power(num, pow - 1));
    }
    return 1;
}
Program Explanation

In this C Program, we are reading a number and its power using ‘num’ and ‘pow’ variables respectively. The power() function is used to find the power of a number using recursion.

advertisement

The power of a number is the number multiplied to itself for the number of times it has been raised to Eg: 73 are 343. Using if condition statement, compute the power of a number. Multiply the number by itself by number of times power raised and return the value. Print the power of a number using printf statement.

Runtime Test Cases
 
$ cc pgm30.c
$ a.out
Enter a number: 456
Enter it's power: 3
456^3 is 94818816

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Free 30-Day C++ Certification Bootcamp is Live. Join Now!

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.