Write a program in java to compute and display the sum of the following series: S = (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + -------- + (1 + 2 + 3 + ----- + n ) / (1 * 2 * 3 * ----- * n)
Answers
Code :-
public class SumOfSeries {
public static int factorial(int n){
if(n==0){
return 1;
}
else{ (1 + 2)1 * 2 * 3)
return (n*factorial(n-1));
}
}
public static int sum(int n){
int result = 0;
for(int i=1; i<=n; i++){
result+=factorial(i);
}
return result;
}
public static void main(String [] args){
System.out.println(sum(*your value of n*)); } }
Output :-
S = (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + -------- + (1 + 2 + 3 + ----- + n ) / (1 * 2 * 3 * ----- * n)
Question:-
Write a program to compute and display the sum of the following series.
S=(1+2)/(1*2) +(1+2+3)/(1*2*3) +....
Program:-
import java.util.*;
class Sum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n=sc.nextInt();
double a=0.0,b=1.0;
for(int i=2;i<=n;i++)
{
a+=i;
b*=i;
s+=a/b;
}
System.out.println("Sum of the series is: "+s);
}
}