Write a java program to display...(use for loop)....
a = 1/2+1/3+1/4....10 terms
Answers
The given problem is solved using language - Java.
public class Sum{
public static void main(String args[]){
double i,s=0;
for(i=1.0;i<=10;i++)
s+=1/(i+1);
System.out.println("Sum of the series upto 10 terms is: "+s);
}
}
- Initialize sum = 0, n = 10.
- Repeat i = 1 to n.
- Add 1/(i + 1) to sum.
- Display the sum.
Sum of the series upto 10 terms is: 2.019877344877345
See attachment for verification.
Answer:
Program:-
public class Main
{
public static void main(String args[])
{
double s=0;
for(int i=2;i<=10;i++)
{
s=s+1/(double)i;
}
System.out.println("The sum upto 10 terms="+s);
}
}
Logic:-
- Run a loop from 2 to 10
- Find the sum of the series use explicit conversion to get the correct value in decimal.
- I even checked in calculator and also I did it in copy it came same as the output.
Logic 2:-
public class Main
{
public static void main(String args[])
{
double s=0;
for(double i=2.0;i<=10.0;i++)
{
s=s+1/i;
}
System.out.println("The sum upto 10 terms="+s);
}
}
In both I got the same output
But,If you mean upto 10 terms then
Program:-
public class Main
{
public static void main(String args[])
{
double s=0;
for(double i=1.0;i<=10.0;i++)
{
s=s+1/(i+1);
}
System.out.println("The sum upto 10 terms="+s);
}
}
In this case output is different.