📌  相关文章
📜  求系列 2、5、8、11、14 的前 N 项的总和。

📅  最后修改于: 2022-05-13 01:56:07.963000             🧑  作者: Mango

求系列 2、5、8、11、14 的前 N 项的总和。

给定一个正整数N ,任务是找到序列的前 N 项的和

例子:

方法:

该序列是通过使用以下模式形成的。对于任何值 N-

上述解决方案可以通过以下一系列步骤得出 -

插图:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to return
// Nth term of the series
int nth(int n, int a1, int d)
{
    return (a1 + (n - 1) * d);
}
 
// Function to return sum of
// Nth term of the series
int sum(int a1, int nterm, int n)
{
    return n * (a1 + nterm) / 2;
}
 
// Driver code
int main()
{
    // Value of N
    int N = 5;
 
    // First term
    int a = 2;
 
    // Common difference
    int d = 3;
 
    // finding last term
    int nterm = nth(N, a, d);
    cout << sum(a, nterm, N) << endl;
    return 0;
}


C
// C program to implement
// the above approach
#include 
 
// Function to return
// Nth term of the series
int nth(int n, int a1, int d)
{
    return (a1 + (n - 1) * d);
}
 
// Function to return
// sum of Nth term of the series
int sum(int a1, int nterm, int n)
{
    return n * (a1 + nterm) / 2;
}
 
// Driver code
int main()
{
    // Value of N
    int N = 5;
 
    // First term
    int a = 2;
 
    // Common difference
    int d = 3;
 
    // Finding last term
    int nterm = nth(N, a, d);
 
    printf("%d", sum(a, nterm, N));
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG {
    // Driver code
    public static void main(String[] args)
    {
        // Value of N
        int N = 5;
 
        // First term
        int a = 2;
 
        // Common difference
        int d = 3;
 
        // Finding Nth term
        int nterm = nth(N, a, d);
        System.out.println(sum(a, nterm, N));
    }
 
    // Function to return
    // Nth term of the series
    public static int nth(int n,
                          int a1, int d)
    {
        return (a1 + (n - 1) * d);
    }
 
    // Function to return
    // sum of Nth term of the series
    public static int sum(int a1,
                          int nterm, int n)
    {
        return n * (a1 + nterm) / 2;
    }
}


Python3
# Python code for the above approach
 
# Function to return
# Nth term of the series
def nth(n, a1, d):
    return (a1 + (n - 1) * d);
 
# Function to return sum of
# Nth term of the series
def sum(a1, nterm, n):
    return n * (a1 + nterm) / 2;
 
# Driver code
 
# Value of N
N = 5;
 
# First term
a = 2;
 
# Common difference
d = 3;
 
# finding last term
nterm = nth(N, a, d);
print((int)(sum(a, nterm, N)))
 
# This code is contributed by gfgking


C#
using System;
 
public class GFG
{
   
    // Function to return
    // Nth term of the series
    public static int nth(int n, int a1, int d)
    {
        return (a1 + (n - 1) * d);
    }
 
    // Function to return
    // sum of Nth term of the series
    public static int sum(int a1, int nterm, int n)
    {
        return n * (a1 + nterm) / 2;
    }
    static public void Main()
    {
 
        // Code
        // Value of N
        int N = 5;
 
        // First term
        int a = 2;
 
        // Common difference
        int d = 3;
 
        // Finding Nth term
        int nterm = nth(N, a, d);
        Console.Write(sum(a, nterm, N));
    }
}
 
// This code is contributed by Potta Lokesh


Javascript



输出:
40