C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop


Lets write a C program to find sum of squares of all the numbers between 1 and user entered number, using while loop.

Related Read:
while loop in C programming

Logic To Find Sum of Squares of Numbers from 1 to N, using While Loop

We ask the user to enter the limit. We initialize variable count to 1. Now inside while loop we square the value of variable count and add it to the previous value of variable sum – we repeat this for each iteration of while loop until value of count is less than or equal to value entered by the user(which is stored in variable N). For each iteration of the while loop value of variable count is incremented by 1.

Video Tutorial: C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop


[youtube https://www.youtube.com/watch?v=YZWcf2lW1sA]

YouTube Link: https://www.youtube.com/watch?v=YZWcf2lW1sA [Watch the Video In Full Screen.]

Source Code: C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop

#include < stdio.h >

int main()
{
    int N, count = 1, sum = 0;

    printf("Enter the limit\n");
    scanf("%d", &N);

    while(count <= N)
    {
        sum = sum + (count * count);
        count++;
    }
    printf("Sum of squares of numbers from 1 to %d is %d.\n", N, sum);

    return 0;
}

Output:
Enter the limit
5
Sum of squares of numbers from 1 to 5 is 55.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *