Numbers Programming QuestionsC# program to check if a number is divisible by 2C# program to accept 2 integers and return remainderC# program to calculate factorial without using recursionC# program to print nth number in Fibonacci seriesC# program to swap two numbers without using a temp variableC# program to check if the entered number is Armstrong numberC# program to find GCD and LCMC# program to check if a number is prime or notC# program to check if a number is Palindromic or notC# program to determine total ways stairs can be climbedC# program to solve FizzBuzz problemC# program to display factor of entered numberPatterns Programming QuestionsC# program to print star triangleC# program to print star triangleC# program to print star triangleC# program to print star diamondC# program to print star M patternC# program to print number triangle-1C# program to print number triangle-2C# program to print number triangle-3C# program to print number triangle-4C# program to print number triangle-5C# program to print number patternC# program to print number diamondC# program to print alphabet patternStrings Programming QuestionsC# program to print duplicate characters in a StringC# program to check if two Strings are anagrams of each otherC# program to reverse String by using Iteration and RecursionC# program to count number of words in a StringC# program to check if String is PalindromeC# program to remove duplicate characters from StringC# program to return highest occurred character in a StringC# program to determine if the string has all unique charactersC# program to replace all spaces in a string with %20C# program to find all substring in a stringGiven a string containing just the characters (, ), {, }, [ and ], determine if the input string is validGiven two words, beginWord and endWord, and a word list, find the length of the shortest transformation sequence from beginWord to endWordRecursion Programming QuestionsC# program to find factorial of a number using recursionC# program to find the sum of digits of a number using recursionC# program to calculate Power of a number using recursionC# program to form the Fibonacci series using recursionC# program to find GCD using recursionC# program to convert a number from Decimal to Binary using recursionC# program to reverse a LinkedList using recursionC# program to do a recursive binary search in an arrayC# program to write recursive Quicksort algorithmC# program to print a singly linked list backwards using recursionC# program to Towers of Hanoi using recursionC# program to print table of any given number by using recursionArray Programming QuestionsC# program to sort an array in ascending orderC# program to sort an array in descending orderC# program to reverse an arrayC# program to find majority element in an unsorted arrayC# program to find missing number in integer array of 1 to 20C# program to merge two sorted arrays into oneC# program to swap min and max element in integer arrayC# program to determine if any two integers in array sum to given integerC# program to check if array contains a duplicate numberC# program to rotate array to a given pivotC# program to move zeros to end of arrayC# program to find the longest common prefix string amongst an array of stringsC# program to find majority number which appears more than 50% in the unsorted arrayData structures Programming QuestionsC# program to reverse a LinkedlistC# program to find node in LinkedlistC# program to merge two sorted LinkedlistC# program to traverse singly LinkedListC# program to traverse circular singly LinkedListC# program to remove duplicates from a sorted LinkedlistC# program to find nth to last element in singly LinkedlistC# program to delete nth element from headnodeC# program to detect a cycle in LinkedlistC# program to implement binary search treeC# program to implement binary search tree TraversalC# program to find min and max in binary search treeC# program to delete nodes from binary search treeC# program to Breadth First Search (BFS)C# program to Depth First Search (DFS)C# program to implement stackC# program to stack with Push & Pop operationC# program to reverse a StackSorting AlgorithmsSelection Sort Algorithm in C#Insertion Sort Algorithm in C#Heap Sort Algorithm in C#Merge Sort Algorithm in C#Quick Sort Algorithm in C#Bubble Sort Algorithm in C#Shell Sort Algorithm in C#Comb Sort Algorithm in C#Bucket Sort Algorithm in C#Radix Sort Algorithm in C#Searching AlgorithmsLinear or Sequntial Search AlgorithmBinary Search AlgorithmLinear vs Binary Search AlgorithmInterpolation Search AlgorithmInterpolation vs Binary Search AlgorithmTernary Search AlgorithmWhy is Binary Search preferred over Ternary Search?Jump Search AlgorithmExponential Search Algorithm

C# program to calculate Power of a number using recursion

Here's a C# program that calculates the power of a number using recursion:


using System;

class Program
{
    static void Main()
    {
        int number = 3;
        int exponent = 4;

        long result = CalculatePower(number, exponent);

        Console.WriteLine(number + " raised to the power of " + exponent + " is: " + result);
    }

    static long CalculatePower(int number, int exponent)
    {
        // Base case: if the exponent is 0, return 1
        if (exponent == 0)
        {
            return 1;
        }
        else
        {
            // Recursive case: calculate the power by multiplying the number 
            // with the power of (number, exponent - 1)
            return number * CalculatePower(number, exponent - 1);
        }
    }
}

Code Explanation

1. The Program Structure

The program starts with the using System; statement, which allows the program to use basic functionalities like Console.WriteLine for printing output to the console.

The Program class contains the Main method, which is the entry point of the program. This is where the program starts executing.

2. The Main Method

Inside the Main method:

  • Two integer variables are declared:
    • number is set to 3. This is the base number we want to raise to a power.
    • exponent is set to 4. This is the power to which we want to raise the number.
  • The CalculatePower method is called with number and exponent as arguments. This method calculates the result of raising number to the power of exponent.
  • The result is stored in a long variable called result (since the result could be a large number).
  • Finally, the program prints the result to the console using Console.WriteLine. The output will look like this:
    3 raised to the power of 4 is: 81

3. The CalculatePower Method

This method is responsible for calculating the power of a number using recursion (a function calling itself).

It takes two parameters:

  • number: The base number.
  • exponent: The power to which the number is raised.

The method has two cases:

  • Base Case:
    • If the exponent is 0, the method returns 1. This is because any number raised to the power of 0 is 1 (e.g., \(3^0 = 1\)).
    • This is the stopping condition for the recursion, preventing it from running indefinitely.
  • Recursive Case:
    • If the exponent is greater than 0, the method calls itself with the same number but with the exponent reduced by 1 (exponent - 1).
    • The result of this recursive call is multiplied by the number and returned.
    • For example:
      • If number = 3 and exponent = 4, the method calculates:
        3 * CalculatePower(3, 3)

4. How the Recursion Works

Let's trace the recursion for number = 3 and exponent = 4:

  1. CalculatePower(3, 4) calls 3 * CalculatePower(3, 3).
  2. CalculatePower(3, 3) calls 3 * CalculatePower(3, 2).
  3. CalculatePower(3, 2) calls 3 * CalculatePower(3, 1).
  4. CalculatePower(3, 1) calls 3 * CalculatePower(3, 0).
  5. CalculatePower(3, 0) returns 1 (base case).
  6. Now, the recursion "unwinds":
    • CalculatePower(3, 1) returns 3 * 1 = 3.
    • CalculatePower(3, 2) returns 3 * 3 = 9.
    • CalculatePower(3, 3) returns 3 * 9 = 27.
    • CalculatePower(3, 4) returns 3 * 27 = 81.

The final result, 81, is returned to the Main method and printed.

5. Summary

The program calculates \(3^4\) using recursion.

The CalculatePower method breaks down the problem into smaller subproblems by reducing the exponent step by step until it reaches the base case.

The result is computed by multiplying the number with the result of the recursive call.

The final output is:



3 raised to the power of 4 is: 81

This is a classic example of how recursion can be used to solve problems by breaking them into smaller, more manageable parts.