public class SumOfPow
{
public static int exponent(int n,int m)
{
int nProd = n;
// COMPLETE THE CODE THAT MAKES THE FUNCTION RETURN n^m
for(int i=1;i<m;i++)
nProd=nProd*n;
return nProd;
}
public static void main(String args[])
{
int N=7, M=4;
int Power = 0;
// Complete the code with the necessary statements. Make use of the method “exponent”
Power=exponent(N,M);
System.out.println ("Sum of Powers: "+Power);
}
} Another easy way to do is by using builtin method...
public class SumOfPow
{
public static void main(String args[])
{
double N=7, M=4;
double Power = 0;
// Complete the code with the necessary statements. Make use of the method “exponent”
Power=Math.pow(N,M);
System.out.println ("Sum of Powers: "+Power);
}
}
To do N^M+ N^(M-1)+ N^(M-2)…. series using builtin method
public class SumOfPow
{
public static void main(String args[])
{
double N=2, M=3;
double Power = 0;
int next=0;
int series=2;
for(int i=0;i<series;i++){
Power=Power+Math.pow(N,(M-next));
next=next+1;
}
System.out.println ("Sum of Powers: "+Power);
}
} Muralidharan.E
Junior Poster in Training
74 posts since Apr 2008
Reputation Points: 32
Solved Threads: 6