Print the first N terms of the series 6, 28, 66, 120, 190, 276, …
Last Updated :
15 Oct, 2021
Given a number N, the task is to print the first N terms of the series 6, 28, 66, 120, 190, 276, and so on.
Examples:
Input: N = 10
Output: 6 28 66 120 190 276 378 496 630 780
Input: N = 4
Output: 6 28 66 120
Approach: To solve the problem mentioned above, we have to observe the below pattern:
The general formula is given by:
k * (2 * k – 1), where initially, k = 2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printSeries( int n)
{
int k = 2;
for ( int i = 0; i < n; i++) {
cout << (k * (2 * k - 1))
<< " " ;
k += 2;
}
cout << endl;
}
int main()
{
int N = 12;
printSeries(N);
return 0;
}
|
Java
class GFG{
static void printSeries( int n)
{
int k = 2 ;
for ( int i = 0 ; i < n; i++)
{
System.out.print(k * ( 2 * k - 1 ) + " " );
k += 2 ;
}
System.out.println();
}
public static void main(String args[])
{
int N = 12 ;
printSeries(N);
}
}
|
Python3
def PrintSeries(n):
k = 2
for i in range ( 0 , n):
print (k * ( 2 * k - 1 ), end = ' ' )
k = k + 2
n = 12
PrintSeries(n)
|
C#
using System;
class GFG{
static void printSeries( int n)
{
int k = 2;
for ( int i = 0; i < n; i++)
{
Console.Write(k * (2 * k - 1) + " " );
k += 2;
}
Console.WriteLine();
}
public static void Main()
{
int N = 12;
printSeries(N);
}
}
|
Javascript
<script>
function printSeries( n)
{
let k = 2;
for (let i = 0; i < n; i++) {
document.write((k * (2 * k - 1))
+ " " );
k += 2;
}
document.writeln( "<br/>" );
}
let N = 12;
printSeries(N);
</script>
|
Output:
6 28 66 120 190 276 378 496 630 780 946 1128
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...