
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to calculate Power of a number using recursion in C#?
To calculate power of a number using recursion, try the following code.
Here, if the power is not equal to 0, then the function call occurs which is eventually recursion −
if (p!=0) { return (n * power(n, p - 1)); }
Above, n is the number itself and the power reduces on every iteration as shown below −
Example
using System; using System.IO; public class Demo { public static void Main(string[] args) { int n = 5; int p = 2; long res; res = power(n, p); Console.WriteLine(res); } static long power (int n, int p) { if (p!=0) { return (n * power(n, p - 1)); } return 1; } }
Output
25
- Related Articles
- Java program to calculate the power of a Given number using recursion
- C++ Program to Calculate Power Using Recursion
- Java Program to calculate the power using recursion
- Golang Program to Calculate The Power using Recursion
- How to Find the Power of a Number Using Recursion in Python?
- Find power of a number using recursion in C#
- C++ program to Calculate Factorial of a Number Using Recursion
- Java program to calculate the GCD of a given number using recursion
- C++ Program to Calculate Power of a Number
- How to calculate power of three using C#?
- Java program to calculate the power of a number
- C program to calculate power of a given number
- Swift Program to Calculate the Power of a Number
- Haskell Program to Calculate the Power of a Number
- Kotlin Program to Calculate the Power of a Number

Advertisements