Number of cells in the Nth order figure

Given an integer **N**, the task is to find the number of cells in **N ^{th}** order figure of the given type:

**Examples:**

Input:N = 2

Output:5

Input:N = 3

Output:13

**Approach:** It can be observed that for the values of **N = 1, 2, 3, …** a series will be formed as **1, 5, 13, 25, 41, 61, 85, 113, 145, 181, …** whose **N ^{th}** term will be

**N**.

^{2}+ (N – 1)^{2}Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number ` `// of cells in the nth order ` `// figure of the given type ` `int` `cntCells(` `int` `n) ` `{ ` ` ` `int` `cells = ` `pow` `(n, 2) + ` `pow` `(n - 1, 2); ` ` ` `return` `cells; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `cout << cntCells(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the number ` `// of cells in the nth order ` `// figure of the given type ` `static` `int` `cntCells(` `int` `n) ` `{ ` ` ` `int` `cells = (` `int` `)Math.pow(n, ` `2` `) + ` ` ` `(` `int` `)Math.pow(n - ` `1` `, ` `2` `); ` ` ` `return` `cells; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `System.out.println(cntCells(n)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the number ` `# of cells in the nth order ` `# figure of the given type ` `def` `cntCells(n) : ` ` ` ` ` `cells ` `=` `pow` `(n, ` `2` `) ` `+` `pow` `(n ` `-` `1` `, ` `2` `); ` ` ` ` ` `return` `cells; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `3` `; ` ` ` ` ` `print` `(cntCells(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number ` `// of cells in the nth order ` `// figure of the given type ` `static` `int` `cntCells(` `int` `n) ` `{ ` ` ` `int` `cells = (` `int` `)Math.Pow(n, 2) + ` ` ` `(` `int` `)Math.Pow(n - 1, 2); ` ` ` `return` `cells; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `Console.WriteLine(cntCells(n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

13

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.