How to write series program in java
1/a2 + 1/a3 + 1/a4-----n
// always focus on first term then see the difference in first and second term
Normal way
double a=3.0, b=2.0,sum=0.0; // "a " value you can take anything
int n=10 // n value you can take any thing
for(int i=1;i<=n;i++)
{
sum=sum+ 1/ Math.pow(a,b);
b++;
}
System.out.println(sum);
Short cut way
double sum=0.0,a=3.0; // "a" value you can take anything
int n=10 // n value you can take any thing
for(int i=1;i<=n;i++)
{
sum=sum+ 1/ Math.pow(a, (i+1));
}
System.out.println(sum);
Comments
Post a Comment