Write a program in Java to find the sum of the following series
S= 2
1 + 42 + 63 +....................n terms
Take necessary input from the user.
Answers
Answer:
Given the value of the n. You have to find the sum of the series where the nth term of the sequence is given by:
Tn = n2 – ( n – 1 )2
Examples :
Input : 3
Output : 9
Explanation: So here the tern of the sequence upto n = 3 are:
1, 3, 5 And hence the required sum is = 1 + 3 + 5 = 9
Input : 6
Output : 36
Simple Approach
Just use a loop and calculate the sum of each term and print the sum.
// CPP program to find summation of series
#include <bits/stdc++.h>
using namespace std;
int summingSeries(long n)
{
// use of loop to calculate
// sum of each term
int S = 0;
for (int i = 1; i <= n; i++)
S += i * i - (i - 1) * (i - 1);
return S;
}
// Driver Code
int main()
{
int n = 100;
cout << "The sum of n term is: "
<< summingSeries(n) << endl;
return 0;
}