Computer Science, asked by Skanda1234, 1 month ago

Write a program in java to compute the following expressions.
S = 1 * ( 1+2) *(1+2+3) * (1+2+3+4)+ ......n
terms
guys it's. urgent
pls reply fast I will mark you the brain list ​

Answers

Answered by prathikshabaleri15
1

Answer:

Input : n = 5

Output : 35

Explanation :

(1) + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) = 35

Input : n = 10

Output : 220

Explanation :

(1) + (1+2) + (1+2+3) + .... +(1+2+3+4+.....+10) = 220

Recommended: Please solve it on PRACTICE first, before moving on to the solution.

Naive Approach :

Below is implementation of above series :

C++

Java

// JAVA Code For Sum of the series

import java.util.*;

class GFG {

// Function to find sum of given series

static int sumOfSeries(int n)

{

int sum = 0;

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

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

sum += j;

return sum;

}

/* Driver program to test above function */

public static void main(String[] args)

{

int n = 10;

System.out.println(sumOfSeries(n));

}

}

// This code is contributed by Arnav Kr. Mandal.

Python

C#

PHP

Output :

220

Efficient Approach :

Let n^{th} term of the series 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4)…(1 + 2 + 3 +..n) be denoted as an

an = Σn1 i = \frac{n (n + 1)}{2} = \frac{(n^2 + n)}{2}

Sum of n-terms of series

Σn1 an = Σn1 \frac{(n^2 + n)}{2}

= \frac{1}{2} Σ [ n^2 ] + Σ [ n ]

= \frac{1}{2} * \frac{n(n + 1)(2n + 1)}{6} + \frac{1}{2} * \frac{n(n+1)}{2}

= \frac{n(n+1)(2n+4)}{12}

Below is implementation of above approach :

C++

// CPP program to find sum of given series

#include <bits/stdc++.h>

using namespace std;

// Function to find sum of given series

int sumOfSeries(int n)

{

return (n * (n + 1) * (2 * n + 4)) / 12;

}

// Driver Function

int main()

{

int n = 10;

cout << sumOfSeries(n);

}

otherwise

Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!

Program for sum of arithmetic series

Series with largest GCD and sum equals to n

Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!

Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))

Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . .

Find the sum of the series 1+11+111+1111+..... upto n terms

Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n

Sum of the series 1, 3, 6, 10... (Triangular Numbers)

Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term

Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)

Sum of series (n/1) + (n/2) + (n/3) + (n/4) +.......+ (n/n)

Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)

Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n

Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n

Sum of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + …… + (2+4+6+8+….+2n)

Similar questions