Microsoft Interview Question
1,272 Interview Reviews |
Back to all Microsoft Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer In Test (SDET) at Microsoft:
How can you write a recursive function calculating the exponential of a number?
See more for this Microsoft Software Development Engineer In Test (SDET) Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (2)
using System;
//Test class
class Test
{
//Constructor
public Test()
{
//Nothing
}
public int RecursiveExp(int x, int n)
{
//First base case
if (n == 0)
{
return 1;
}
//Second base case
if (n == 1)
{
return x;
}
//Even values of (n)
if (n % 2 == 0)
{
int y = RecursiveExp(x, n / 2);
return y * y;
}
//Odd values of (n)
else
{
int y = RecursiveExp(x, n - 1);
return x * y;
}
}
}
//Main class
class Program
{
//Main
static void Main(string[] args)
{
//Create a test object
Test tst = new Test();
//Examples
Console.Out.WriteLine(tst.RecursiveExp(2, 0));
Console.Out.WriteLine(tst.RecursiveExp(2, 1));
Console.Out.WriteLine(tst.RecursiveExp(2, 3));
Console.Out.WriteLine(tst.RecursiveExp(2, 4));
}
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Vinay Bharadwaj:
f(n) = a*f(n-1) otherwise.