Function Recursive Power - C# Programming Exercise

In this C# exercise, you are asked to write a function that calculates the result of raising one integer to another integer using recursion. For example, raising 5 to the 3rd power results in 125, as 5 raised to 3 is 5 × 5 × 5. This exercise is essential for understanding how mathematical problems can be solved using recursion, a programming technique where a function calls itself to solve a problem in smaller parts. You must define the function in such a way that the power calculation is performed recursively, without using iterative structures. This exercise will help you better understand the concept of recursion in C# and how to implement it correctly to solve problems efficiently.

 Category

Functions

 Exercise

Function Recursive Power

 Objective

Write a C# function that calculates the result of raising an integer to another integer (eg 5 raised to 3 = 53 = 5 × 5 × 5 = 125). This function must be created recursively.

An example of use would be: Console.Write( Power(5,3) );

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Recursive function to calculate the power of a number raised to another number
    public static int Power(int baseNumber, int exponent)
    {
        // Base case: If the exponent is 0, return 1 (any number raised to the power of 0 is 1)
        if (exponent == 0)
        {
            return 1;
        }

        // Recursive case: Multiply the base number by the result of calling Power with the exponent - 1
        return baseNumber * Power(baseNumber, exponent - 1);
    }

    // Main method where the Power function is called
    public static void Main()
    {
        // Declare two integers: base and exponent
        int baseNumber = 5;
        int exponent = 3;

        // Call the recursive Power function and print the result
        Console.WriteLine("The result of {0} raised to the power of {1} is: {2}", baseNumber, exponent, Power(baseNumber, exponent));
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function Fibonacci

    In this C# exercise, you are asked to write a program that uses recursion to calculate a number in the Fibonacci series. The Fibonacci series is a seque...

  •  Function modify a letter in a string

    In this exercise of C#, you will learn how to create a function named ChangeChar that modifies a letter at a specific position (starting from 0) in a st...

  •  Function IsPrimeTarea

    In this exercise of C#, you will learn how to create a function named IsPrime that receives an integer number and returns true if the number is p...

  •  Function Parameters of Main, Sum

    In this exercise of C#, you will learn how to create a program named sum that receives two integer numbers from the command line and displays their sum. This t...

  •  Function SumDigits

    In this exercise of C#, you will learn how to create a function named SumDigits that receives a number and returns the result of summing its digits. Thi...

  •  Function Factorial

    In this exercise of C#, you will learn how to create a recursive function to calculate the factorial of a number. The factorial of a number is ex...