
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to calculate power of three using C#?
For power of 3, se the power as 3 and apply a recursive code like the following snippet −
if (p!=0) { return (n * power(n, p - 1)); }
Let’s say the number is 5, then the iterations would be −
power(5, 3 - 1)); // 25 power (5,2-1): // 5
The above would return5*25 i.e 125 as shown below −
Example
using System; using System.IO; public class Demo { public static void Main(string[] args) { int n = 5; int p = 3; 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
125
Advertisements