📜  打印系列6、28、66、120、190、276等的前N个术语

📅  最后修改于: 2021-04-29 10:44:25             🧑  作者: Mango

给定的数N,所述任务是打印一系列6,28,66,120,190,276,等等的前N项。
例子:

方法:要解决上述问题,我们必须遵循以下模式:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the series
void printSeries(int n)
{
    // Initialise the value of k with 2
    int k = 2;
 
    // Iterate from 1 to n
    for (int i = 0; i < n; i++) {
 
        // Print each number
        cout << (k * (2 * k - 1))
             << " ";
 
        // Increment the value of
        // K by 2 for next number
        k += 2;
    }
 
    cout << endl;
}
 
// Driver Code
int main()
{
    // Given number N
    int N = 12;
 
    // Function Call
    printSeries(N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to print the series
static void printSeries(int n)
{
    // Initialise the value of k with 2
    int k = 2;
 
    // Iterate from 1 to n
    for (int i = 0; i < n; i++)
    {
 
        // Print each number
        System.out.print(k * (2 * k - 1) + " ");
 
        // Increment the value of
        // K by 2 for next number
        k += 2;
    }
 
    System.out.println();
}
 
// Driver code
public static void main(String args[])
{
    // Given number N
    int N = 12;
 
    // Function Call
    printSeries(N);
}
}
 
// This code is contributed by shivaniisnghss2110


Python3
# Python3 program for the above apporch
 
# Function to print the series
def PrintSeries(n):
     
    # Initialise the value of k with 2
    k = 2
     
    # Iterate from 1 to n
    for i in range(0, n):
         
        # Print each number
        print(k * (2 * k - 1), end = ' ')
         
        # Increment the value of
        # K by 2 for next number
        k = k + 2
         
# Driver code    
 
# Given number
n = 12
 
# Function Call
PrintSeries(n)
 
# This code is contributed by poulami21ghosh


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to print the series
static void printSeries(int n)
{
     
    // Initialise the value of k with 2
    int k = 2;
 
    // Iterate from 1 to n
    for(int i = 0; i < n; i++)
    {
 
        // Print each number
        Console.Write(k * (2 * k - 1) + " ");
 
        // Increment the value of
        // K by 2 for next number
        k += 2;
    }
    Console.WriteLine();
}
 
// Driver code
public static void Main()
{
     
    // Given number N
    int N = 12;
 
    // Function call
    printSeries(N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
6 28 66 120 190 276 378 496 630 780 946 1128

时间复杂度: O(N)
辅助空间: O(1)