Computer Science, asked by MalayaDhal, 5 months ago

Write a program to find a sum in java (Use for loop):-
S=1+(1*2)+(1*2*3)......+ n terms.
Solve it fast....​

Answers

Answered by ankoolsrivastava
1

Answer:

Given a positive integer n, write a function to compute sum of the series 1/1! + 1/2! + .. + 1/n!

A Simple Solution solution is to initialize sum as 0, then run a loop and call factorial function inside the loop.

Following is the implementation of simple solution.

// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n!

#include <iostream>

using namespace std;

// Utility function to find

int factorial(int n)

{

int res = 1;

for (int i=2; i<=n; i++)

res *= i;

return res;

}

// A Simple Function to return value of 1/1! + 1/2! + .. + 1/n!

double sum(int n)

{

double sum = 0;

for (int i = 1; i <= n; i++)

sum += 1.0/factorial(i);

return sum;

}

// Driver program to test above functions

int main()

{

int n = 5;

cout << sum(n);

Answered by anindyaadhikari13
4

Question:-

Write a program in java to find the sum of the series.

S = 1 + (1*2) + (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();

long p=1,s=0;

for(int i=1;i<=n;i++)

{

p*=i;

s+=p;

}

System.out.println("Sum of the series is: "+s);

}

}

Similar questions