- 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
Find power of a number using recursion in C#
To find the power of a number, firstly set the number and the power −
int n = 15; int p = 2;
Now create a method and pass these values −
static long power(int n, int p) { if (p != 0) { return (n * power(n, p - 1)); } return 1; }
Above, the recursive call gave us the results −
n * power(n, p - 1)
The following is the complete code to get the power of a number −
Example
using System; using System.IO; public class Demo { public static void Main(string[] args) { int n = 15; 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
225
Advertisements