📌  相关文章
📜  程序打印给定第n个项的总和

📅  最后修改于: 2021-05-07 09:03:09             🧑  作者: Mango

给定n的值。您必须找到序列的总和,其中序列的第n项由下式给出:
T n = n 2 –(n – 1) 2
例子 :

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

简单方法
只需使用一个循环并计算每个项的总和并打印总和即可。

C++
// CPP program to find summation of series
#include 
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;
}


Java
// JAVA program to find summation of series
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
 
class GFG
{
 
    // function to calulate sum of series
    static int summingSeries(long n)
    {
        // use of loop to calculate
        // sum of each term
        int S = 0;
        for (i = 1; i <= n; i++)
            S += i * i - (i - 1) * (i - 1);    
         
        return S;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 100;
        System.out.println("The sum of n term is: " +
                            summingSeries(n));
    }
}


Python3
# Python3 program to find summation
# of series
 
def summingSeries(n):
 
    # use of loop to calculate
    # sum of each term
    S = 0
    for i in range(1, n+1):
        S += i * i - (i - 1) * (i - 1)
     
    return S
 
# Driver Code
n = 100
print("The sum of n term is: ",
           summingSeries(n), sep = "")
# This code is contributed by Smitha.


C#
// C# program to illustrate...
// Summation of series
using System;
 
class GFG
{
 
    // function to calculate sum of series
    static int summingSeries(long n)
    {
 
        // Using the pow function calculate
        // the sum of the series
        return (int)Math.Pow(n, 2);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 100;
        Console.Write("The sum of n term is: " +
                        summingSeries(n));
    }
}
 
// This code contribute by Parashar...


PHP


Javascript


C++
// CPP program to illustrate...
// Summation of series
 
#include 
using namespace std;
 
int summingSeries(long n)
{
    // Sum of n terms is n^2
    return pow(n, 2);
}
 
// Driver Code
int main()
{
    int n = 100;
    cout << "The sum of n term is: "
         << summingSeries(n) << endl;
    return 0;
}


Java
// JAVA program to illustrate...
// Summation of series
 
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
 
class GFG
{
 
    // function to calculate sum of series
    static int summingSeries(long n)
    {
 
        // Using the pow function calculate
        // the sum of the series
        return (int)Math.pow(n, 2);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 100;
        System.out.println("The sum of n term is: " +
                            summingSeries(n));
    }
}


Python3
# Python3 program to illustrate...
# Summation of series
import math
 
def summingSeries(n):
 
    # Sum of n terms is  n^2
    return math.pow(n, 2)
 
# Driver Code
n = 100
print ("The sum of n term is: ",
        summingSeries(n))
# This code is contributed by mits.


C#
// C# program to illustrate...
// Summation of series
using System;
 
class GFG
{
    // function to calculate sum of series
    static int summingSeries(long n)
    {
        // Using the pow function calculate
        // the sum of the series
        return (int)Math.Pow(n, 2);
    }
 
    // Driver code
    public static void Main()
    {
        int n = 100;
           Console.Write("The sum of n term is: " +
                              summingSeries(n));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出:

The sum of n term is: 10000

时间复杂度– O(N)
空间复杂度– O(1)
高效方法
使用数学方法可以更有效地解决此问题。

C++

// CPP program to illustrate...
// Summation of series
 
#include 
using namespace std;
 
int summingSeries(long n)
{
    // Sum of n terms is n^2
    return pow(n, 2);
}
 
// Driver Code
int main()
{
    int n = 100;
    cout << "The sum of n term is: "
         << summingSeries(n) << endl;
    return 0;
}

Java

// JAVA program to illustrate...
// Summation of series
 
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
 
class GFG
{
 
    // function to calculate sum of series
    static int summingSeries(long n)
    {
 
        // Using the pow function calculate
        // the sum of the series
        return (int)Math.pow(n, 2);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 100;
        System.out.println("The sum of n term is: " +
                            summingSeries(n));
    }
}

Python3

# Python3 program to illustrate...
# Summation of series
import math
 
def summingSeries(n):
 
    # Sum of n terms is  n^2
    return math.pow(n, 2)
 
# Driver Code
n = 100
print ("The sum of n term is: ",
        summingSeries(n))
# This code is contributed by mits.

C#

// C# program to illustrate...
// Summation of series
using System;
 
class GFG
{
    // function to calculate sum of series
    static int summingSeries(long n)
    {
        // Using the pow function calculate
        // the sum of the series
        return (int)Math.Pow(n, 2);
    }
 
    // Driver code
    public static void Main()
    {
        int n = 100;
           Console.Write("The sum of n term is: " +
                              summingSeries(n));
    }
}
 
// This code is contributed by nitin mittal.

的PHP


Java脚本


输出:

The sum of n term is: 10000

时间复杂度– O(1)
空间复杂度– O(1)