C C++ program to find power of number using recursion

Coding in C

#include<stdio.h>
int power(int,int);
int main()
{
    int num,pow,c=1,res=1;
    printf("Enter number: ");
    scanf("%d",&num);
    printf("Enter power : ");
    scanf("%d",&pow);
    printf("Result = %d",power(num,pow));
    return 0;
}
int power(int num, int pow)
{
    if(pow==0)
        return 1;
    else
        return num*power(num,pow-1);
}

Coding in C++

#include<iostream>
using namespace std;
int power(int,int);
int main()
{
    int num,pow,c=1,res=1;
    cout<<"Enter number: ";
    cin>>num;
    cout<<"Enter power : ";
    cin>>pow;
    cout<<"Result = "<<power(num,pow);
    return 0;
}
int power(int num, int pow)
{
    if(pow==0)
        return 1;
    else
        return num*power(num,pow-1);
}

Output

C C++ program to find power of number using recursion

Published by

admin

I am a computer teacher, programmer and web developer