Program to print the series 1, 9, 17, 33, 49, 73, 97… till N terms

Given a number N, the task is to print the first N terms of the series:

1, 9, 17, 33, 49, 73, 97....

Examples:

Input: N = 7
Output: 1, 9, 17, 33, 49, 73, 97

Input: N = 3
Output: 1, 9, 17



Approach: From the given series, find the formula for Nth term:

1st term = 1
2nd term = 9 = 2 * 4 + 1
3rd term = 17 = 2 * 9 - 1
4th term = 33 = 2 * 16 + 1
5th term = 49 = 2 * 25 - 1
6th term = 73 = 2 * 36 + 1
.
.
Nth term = (2 * N2 + (-1)N)

Therefore:

Nth term of the series 1, 9, 17, 33, 49, 73, 97.... = (2*N^2 + (-1)^N)

Then iterate over numbers in the range [1, N] to find all the terms using the above formula and print them.

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
  
#include "bits/stdc++.h"
using namespace std;
  
// Function to print the series
void printSeries(int N)
{
  
    int ith_term = 0;
  
    // Generate the ith term and
    // print it
    for (int i = 1; i <= N; i++) {
  
        ith_term = i % 2 == 0
                       ? 2 * i * i + 1
                       : 2 * i * i - 1;
        cout << ith_term << ", ";
    }
}
  
// Driver Code
int main()
{
    int N = 7;
  
    printSeries(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.*;
  
class GFG{
   
// Function to print the series
static void printSeries(int N)
{
   
    int ith_term = 0;
   
    // Generate the ith term and
    // print it
    for (int i = 1; i <= N; i++) {
   
        ith_term = i % 2 == 0
                       ? 2 * i * i + 1
                       : 2 * i * i - 1;
        System.out.print(ith_term+ ", ");
    }
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 7;
   
    printSeries(N);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the above approach
  
# Function to prthe series
def printSeries(N):
  
    ith_term = 0;
  
    # Generate the ith term and
    # prit
    for i in range(1,N+1):
  
        ith_term = 0;
        if(i % 2 == 0):
            ith_term = 2 * i * i + 1;
        else:
            ith_term = 2 * i * i - 1;
        print(ith_term,end= ", ");
      
# Driver Code
if __name__ == '__main__':
    N = 7;
  
    printSeries(N);
      
# This code is contributed by Princi Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG{
  
// Function to print the series
static void printSeries(int N)
{
  
    int ith_term = 0;
  
    // Generate the ith term and
    // print it
    for (int i = 1; i <= N; i++) {
  
        ith_term = i % 2 == 0? 2 * i * i + 1: 
                                2 * i * i - 1;
        Console.Write(ith_term+ ", ");
    }
}
  
// Driver Code
public static void Main()
{
    int N = 7;
  
    printSeries(N);
}
}
  
// This code is contributed by AbhiThakur

chevron_right


Output:

1, 9, 17, 33, 49, 73, 97,

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.