S=1/2+2/3+3/4+....to n terms
Answers
HELLO THERE!
Here's the program code in JAVA:
I'm using Scanner class to input the value of n from the user..
import java.util.*;
class Series
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int n;
double sum = 0.0;
System.out.print("Enter the number of terms: ");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
sum = sum + (i/(i+1));
}
System.out.println("Sum of the series is: " + sum);
}
}
THANKS!
Here is the answer :
Code
import java.util.*;
class Sum_series
{
public static void main(String[] args)
{
double i, s = 0.0;
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the last number: ");
n = sc.nextInt();
for(i = 1.0; i < n; i++)
{
s=s+ (i*1.0)/(i + 1.0);
}
System.out.println("sum="+s);
}
}
Working :
- The i-loop starts from 1.0 then increases by 1.0 each time
- It goes on until n-1
- The printing is done by s such that s = i/(i+1.0)
- Well this prints the sum of 1/2+2/3.............
Hope it helps you
______________________________________________________________________