In java only
Please give correct answer for the series , I have exam tomorrow If this is a so answer, I will report you
THIS IS A VERY SERIOUS QUESTION
Answers
Answer:
I leave few things for you.
1) taking input from user, i have fixed it to 5 here.
2) 2nd program, both program are same with little variation.
3) take care of data type as value i think will rise exponentially as x goes bigger
You need to understand the looping structure ie, from where to start and where to end. If you observe 1st problem, for the first element you can write x power 1 upon 1 factorial. nth number in series follows the same pattern. 5th element would be x power 5, upon 5 factorial.
public class Calculate{
void calculateTotal()
{
int x= 5,counter ;
double total = 0.0;
for(counter = 1; counter<=x;counter++)
total += Math.pow(x,counter)/fact(counter);
System.out.println("Total : " + total);
}
public double fact(int num){
if(num == 1)
return 1;
else
return num*fact(num-1);
}
public static void main(String args[])
{
Calculate c = new Calculate();
c.calculateTotal();
}
}
Highlighted line is the heart of the program.
total += Math.pow(x,counter)/fact(counter);
for 2nd program, only this would change, it would be
total += Math.pow(x,counter)/fact(counter+1);
Hope it helps