How to write program 1/2+2/3+3/4------n terms
1/2+2/3+3/4------n terms
// always focus on first term then see the difference in first and second term
Normal way
double a=1.0, b=2.0,sum=0.0;
int n=10 // n value you can take any thing
for(int i=1;i<=n;i++)
{
sum=sum+ a/b;
a++;
b++;
}
System.out.println(sum);
Short cut way
double sum=0.0;
int n=10 // n value you can take any thing
for(int i=1;i<=n;i++)
{
sum=sum+ i/(i+1);
}
System.out.println(sum);
Comments
Post a Comment