Squared triangular number ( Sum of cubes ) | C ++ program to check if a given number is sum of cubes of natural numbers . ; Function to find if the given number is sum of the cubes of first n natural numbers ; Start adding cubes of the numbers from 1 ; If sum becomes equal to s return n ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return -1 ; } int main ( ) { int s = 9 ; int n = findS ( s ) ; n == -1 ? cout << " - 1" : cout << n ; return 0 ; }