Related Articles

# Program to check if N is a Centered Cubic Number

• Last Updated : 24 Mar, 2021

Given a number N, the task is to check if N is a centered cubic number or not.

A centred cubic number counts the number of points which are formed by a point that is surrounded by concentric cubical layers in 3D with i2 points on the square faces of the i-th layer. The first few Centered cube numbers are 1, 9, 35, 91, 189, 341, 559, 855 …

Examples:

Input: N = 9
Output: Yes
Explanation:
9 is the second Centered cube number
Input: N = 6
Output: No

Approach: The idea is to iterate from one and check whether the ith term is equal to N or not.

1. The Nth term of a centered cubic number is given by .
2. Run a loop starting from 1, to find ith centered cube number.
3. Check if the i-th term is equal to N or not. If it is equal, then return true.
4. If i-th term is greater than N, then return false.

Below is the implementation of the above approach:

## C++

 `// C++ program to check if N``// is a centered cubic number``#include ``using` `namespace` `std;` `// Function to check if the number N``// is a centered cubic number``bool` `isCenteredcube(``int` `N)``{``// Iterating from 1``    ``int` `i = 1;` `// Infinite loop``    ``while` `(``true``) {` `        ``// Finding ith_term``        ``int` `ith_term = (2 * i + 1)``* (i * i + i + 1);` `        ``// Checking if the number N``        ``// is a Centered cube number``        ``if` `(ith_term == N) {``            ``return` `true``;``        ``}` `        ``// If ith_term > N then``        ``// N is not a Centered cube number``        ``if` `(ith_term > N) {``            ``return` `false``;``        ``}` `        ``// Incrementing i``        ``i++;``    ``}``}` `// Driver code``int` `main()``{``    ``int` `N = 9;` `    ``// Function call``    ``if` `(isCenteredcube(N)) {``        ``cout << ``"Yes"``;``    ``}``    ``else` `{``        ``cout << ``"No"``;``    ``}``    ``return` `0;``}`

## Java

 `// Java program to check if N``// is a centered cubic number``class` `GFG{` `// Function to check if N``// is a centered cubic number``static` `boolean` `isCenteredcube(``int` `N)``{``    ` `    ``// Iterating from 1``    ``int` `i = ``1``;``    ` `    ``// Infinite loop``    ``while` `(``true``)``    ``{``        ` `        ``// Finding ith_term``        ``int` `ith_term = (``2` `* i + ``1``) *``                       ``(i * i + i + ``1``);` `        ``// Checking if the number N``        ``// is a centered cube number``        ``if` `(ith_term == N)``        ``{``            ``return` `true``;``        ``}` `        ``// If ith_term > N then N is ``        ``// not a centered cube number``        ``if` `(ith_term > N)``        ``{``            ``return` `false``;``        ``}` `        ``// Incrementing i``        ``i++;``    ``}``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``9``;` `    ``// Function call``    ``if` `(isCenteredcube(N))``    ``{``        ``System.out.println(``"Yes"``);``    ``}``    ``else``    ``{``        ``System.out.println(``"No"``);``    ``}``}``}` `// This code is contributed by shubham`

## Python3

 `# Python3 program to check if N``# is a centered cubic number` `# Function to check if N``# is a centered cubic number``def` `isCenteredcube(N):` `    ``# Iterating from 1``    ``i ``=` `1``;``    ` `    ``# Infinite loop``    ``while` `(``True``):``    ` `        ``# Finding ith_term``        ``ith_term ``=` `((``2` `*` `i ``+` `1``) ``*``                    ``(i ``*` `i ``+` `i ``+` `1``));` `        ``# Checking if the number N``        ``# is a centered cube number``        ``if` `(ith_term ``=``=` `N):``            ``return` `True``;``        ` `        ``# If ith_term > N then N is``        ``# not a centered cube number``        ``if` `(ith_term > N):``            ``return` `False``;``        ` `        ``# Incrementing i``        ``i ``+``=` `1``;``    ` `# Driver code``N ``=` `9``;` `# Function call``if` `(isCenteredcube(N)):``    ``print``(``"Yes"``);``else``:``    ``print``(``"No"``);` `# This code is contributed by Code_Mech`

## C#

 `// C# program to check if N``// is a centered cubic number``using` `System;``class` `GFG{` `// Function to check if N``// is a centered cubic number``static` `Boolean isCenteredcube(``int` `N)``{``    ` `    ``// Iterating from 1``    ``int` `i = 1;``    ` `    ``// Infinite loop``    ``while` `(``true``)``    ``{``        ` `        ``// Finding ith_term``        ``int` `ith_term = (2 * i + 1) *``                       ``(i * i + i + 1);` `        ``// Checking if the number N``        ``// is a centered cube number``        ``if` `(ith_term == N)``        ``{``            ``return` `true``;``        ``}` `        ``// If ith_term > N then N is``        ``// not a centered cube number``        ``if` `(ith_term > N)``        ``{``            ``return` `false``;``        ``}` `        ``// Incrementing i``        ``i++;``    ``}``}` `// Driver code``public` `static` `void` `Main()``{``    ``int` `N = 9;``    ` `    ``// Function call``    ``if` `(isCenteredcube(N))``    ``{``        ``Console.WriteLine(``"Yes"``);``    ``}``    ``else``    ``{``        ``Console.WriteLine(``"No"``);``    ``}``}``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``
Output:
`Yes`

Time Complexity: O(N).

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up