Program to find sum of squares of first n natural numbers using for loop

Logic:

  • Enter the number n
  • sum= 12 + 22+ 32 + 4 2+……..+n2

Algorithm

  1. Enter the number as n
  2. Initially assign sum=0
  3. for(i=1;i<=n;i++)
  4. sum=sum + i * i;
  5. Print sum

Program

#include<stdio.h>
void main()
{
    
    printf("**************************************************************");
    printf("\n**************************************************************");
    printf("\n**  WAP to print sum of squares of first n natural numbers  **");
    printf("\n**                 Created by Sheetal Garg                  **");
    printf("\n**                    Assistant Professor                   **");
    printf("\n**                    Phone No:9467863365                   **");
    printf("\n**************************************************************");
    printf("\n**************************************************************\n");
    int i,n,s=0;
    printf("Enter number upto which square you want to add\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    s=s+i*i;
    printf("sum is %d",s);
}

Output 1

***************************************************************************
***************************************************************************
**       WAP to print sum of squares of first n natural numbers          **
**                          Created by Sheetal Garg                      **
**                            Assistant Professor                        **
**                            Phone No:9467863365                        **
***************************************************************************
***************************************************************************
Enter number upto which square you want to add
5
sum is 55
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.