C Program To Print 1+4+9+16 Series, using For Loop


Lets write C program to print/display number series 1 + 4 + 9 + 16 + 25 + using for loop.

Related Read:
For Loop In C Programming Language
C Program To Print 1+4+9+16 Series, using While Loop

Video Tutorial: C Program To Print 1+4+9+16+25 Series, using for Loop


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

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

Logic To Print 1+4+9+16 number series using for loop

If we analyze the number series, its just addition of square of natural numbers. i.e., (1 x 1) + (2 x 2) + (3 x 3) + (4 x 4) + (5 x 5) + .. etc

We ask the user to enter a number. If user enters num = 5, then we display the first 5 numbers in the series i.e., 1 + 4 + 9 + 16 + 25 +

If user enters num = 10, then we display the first 10 numbers in the series i.e., 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 +

For Loop Logic

We initialize count to 1, as the number series starts from 1. We iterate for loop until count is less than or equal to user entered limit. For each iteration of the for loop we increment the value of count by 1.

Inside for loop we square the value of count and print the result, for each iteration of for loop.

Source Code: C Program To Print 1+4+9+16 Series, using for Loop

#include<stdio.h>

int main()
{
    int limit, count;

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

    for(count = 1; count <= limit; count++)
    {
        printf("%d + ", (count*count));
    }
    printf("\n");

    return 0;
}

Output 1:
Enter the limit
5
1 + 4 + 9 + 16 + 25 +

Output 2:
Enter the limit
10
1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 +

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 *