Home » Sum of squares of N natural numbers in C using formula

Sum of squares of N natural numbers in C using formula

Learn about Sum of squares of N natural numbers in C using formula in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

Sum of squares of N natural numbers in C without using loop

Program:

#include<stdio.h>
int main()
{
  int n;

  printf("Enter n value: ");
  scanf("%d", &n);

  int sum = n*(n+1)*(2*n+1)/6;

  printf("Sum of squares of first %d natural numbers = %d", n, sum);

  return 0;
}

Output:

Enter n value: 5
Sum of squares of first 5 natural numbers = 55

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Sum of squares of N natural numbers in C using while loop
Sum of squares of N natural numbers in C using for loop