

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 find the power of any given number by backtracking using C#?
Create a function to Find Power which takes the number x and n, where x is the 2 and n is how many times, we have to do the power. If the number is even then we have to do x*x and if the number is odd multiply the result with x*x. Continue the recursive call until the n becomes 0.
Suppose if we have a number 2 and 8, then 2*2*2*2*2*2*2*2 =256.
Example
using System; namespace ConsoleApplication{ public class BackTracking{ public int FindPower(int x, int n){ int result; if (n == 0){ return 1; } result = FindPower(x, n / 2); if (n % 2 == 0){ return result * result; } else{ return x * result * result; } } } class Program{ static void Main(string[] args){ BackTracking b = new BackTracking(); int res = b.FindPower(2, 8); Console.WriteLine(res); } } }
Output
256
- Related Questions & Answers
- How to find the target sum from the given array by backtracking using C#?
- How to find the distinct subsets from a given array by backtracking using C#?
- How to find all the permutation of the string by backtracking using C#?
- Find power of a number using recursion in C#
- Find the slope of the given number using C++
- How to Find the Power of a Number Using Recursion in Python?
- C program to calculate power of a given number
- Java program to calculate the power of a Given number using recursion
- How to get all the combinations of the keypad value in a mobile by backtracking using C#?
- How to find the number of digits in a given number using Python?
- Find the Numbers that are not divisible by any number in the range [2, 10] using C++
- How to calculate Power of a number using recursion in C#?
- How to find power of a number in Python?
- How to find the product of given digits by using for loop in C language?
- C++ Program to find whether a number is the power of two?
Advertisements