Interview Question
x +ve, 0,-ve
n +ve, o,-ve
check when n=0, irrespective of x, the return value==1.
check for n=1, then x should be returned.
n=2 -> x*x is your expected output irrespective of sign of x,
n=3, x=-v3, then x cube with a -ve sign should be returned..
** when n becomes negative, if x is zero, its a divide by zero exception
n is -ve, and x is +ve or -ve, the result is always <1, so if the return value is an int,its going to be rounded to a 0 or 1..
** a test case for the overflow.. if x and n are large enough intgers, the overflow will cause the sign bit to on.
suppose we want to calculate x^n then
- ALGORITHMIST February 02, 20111.Check both values are inputted correctly or not.
2.Check for trivial case when x=0 then for any value of n it should return 0.
3.when n=0 it should always return 1 whatever x be.
4.check for negative values of x and n.
i)If n is negative then result should be always less than or equal to x.
5.Check first for smaller values of x and n i.e for 2,2 3,4 etc.
6.Now check for larger values of n and x and see the results.