write a java program to find the value of s s=1 +1+2/1*2 + 1+2+3/1*2*3 +1+2+3+4 /1*2*3*4 + …………………………………..1+2+3+4+………..n /1*2*3*4*………….n
Answers
Answered by
1
Language:
JAVA
Program:
import java.util.Scanner;
public class Program
{public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int n = scan.nextInt();
double fin=0;
for(int i=1;i<=n;i++)
{ double pro =1;
double sum=0;
for (int j =1;j<=i; j++)
{ pro =j*pro;
sum = sum+ j; }
fin = fin + (sum/pro);}
System.out.println(fin); }}
Explanation:
- Take n as input
- Run a loop to get numbers for sum and products.
- Run a nested loop to get the sum of each term.
- Print the output.
Note: Please ensure datatype of set to double or you'll end up making the mistake I did.
Output:
For n = 4
Attachments:
Attachments:
Similar questions