- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a recursive method call in C#?
Recursive method call in C# is called Recursion. Let us see an example to calculate power of a number using recursion.
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; } }
Advertisements