Related Articles
Nicomachu’s Theorem
• Difficulty Level : Easy
• Last Updated : 06 Apr, 2021

Nicomachu’s Theorem states that sum of cubes of first n natural numbers is equal to squares of natural number sum. In other words Or we can say that the sum is equal to square of n-th triangular number.
Mathematical Induction based proof can be found here

## C++

 // CPP program to verify Nicomachu's Theorem#include using namespace std; void NicomachuTheorum_sum(int n){   // Compute sum of cubes   int sum = 0;   for (int k=1; k<=n; k++)      sum += k*k*k;       // Check if sum is equal to   // given formula.   int triNo = n*(n+1)/2;   if (sum == triNo * triNo)     cout << "Yes";   else     cout << "No";} // driver functionint main(){    int n = 5;    NicomachuTheorum_sum(n);    return 0;}

## Java

 // Java program to verify Nicomachu's Theoremimport java.io.*; class GFG {     static void NicomachuTheorum_sum(int n)    {                 // Compute sum of cubes        int sum = 0;                 for (int k = 1; k <= n; k++)            sum += k * k * k;                     // Check if sum is equal to        // given formula.        int triNo = n * (n + 1) / 2;                 if (sum == triNo * triNo)            System.out.println("Yes");        else            System.out.println("No");    }         // driver function    public static void main (String[] args)    {        int n = 5;        NicomachuTheorum_sum(n);    }} // This code is contributed by anuj_67.

## Python3

 # Python3 program to verify# Nicomachu's Theorem def NicomachuTheorum_sum(n):         # Compute sum of cubes    sum = 0;    for k in range(1, n + 1):        sum += k * k * k;             # Check if sum is equal to    # given formula.    triNo = n * (n + 1) / 2;    if (sum == triNo * triNo):        print("Yes");    else:        print("No"); # Driver Coden = 5;NicomachuTheorum_sum(n); # This code is contributed# by mits

## C#

 // C# program to verify// Nicomachu's Theoremusing System;  class GFG {      static void NicomachuTheorum_sum(int n)    {                  // Compute sum of cubes        int sum = 0;                  for (int k = 1; k <= n; k++)            sum += k * k * k;                      // Check if sum is equal to        // given formula.        int triNo = n * (n + 1) / 2;                  if (sum == triNo * triNo)            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }          // Driver Code    public static void Main ()    {        int n = 5;        NicomachuTheorum_sum(n);    }}  // This code is contributed by anuj_67

## PHP

 

## Javascript

 
Output:
Yes

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up