- Related Questions & Answers
- Average numbers in array in C Programming
- C Program for Tower of Hanoi
- C++ Program for Dijkstra’s shortest path algorithm?
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C program to find the length of a string?
- C Program to Check if all digits of a number divide it
- C Program to Check if a Given String is a Palindrome?
- Write a program to Delete a Tree in C programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming
- Sum of the nodes of a Singly Linked List in C Program
- Sum of first n natural numbers in C Program
- C Program for simple interest?
- Area of a n-sided regular polygon with given Radius in C Program?

The sum of squares of the first n even numbers means that, we first find the square and add all them to give the sum.

There are two methods to find the sum of squares of the first n even number

We can use loops to iterate from 1 to n increase number by 1 each time find the square and add it to the sum variable −

#include <iostream> using namespace std; int main() { int sum = 0, n =12; for (int i = 1; i <= n; i++) sum += (2 * i) * (2 * i); cout <<"Sum of first "<<n<<" natural numbers is "<<sum; return 0; }

Sum of first 12 natural numbers is 2600

The complexity of this program increase by order 0(n). So, for big values of n, code takes time.

To deal with this problem a mathematical formula is derived which is Sum of even natural number is 2n(n+1)(2n+1)/3

#include <iostream> using namespace std; int main() { int n = 12; int sum = (2*n*(n+1)*(2*n+1))/3; cout <<"Sum of first "<<n<<" natural numbers is "<<sum; return 0; }

Sum of first 12 natural numbers is 2600

Advertisements