UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to find the sum of first 10 integers using for loop

C program to find the sum first of 10 integer numbers using for loop. Find sum 1 - 10 using for loop

by Krishna viswambharan


Posted on 10 Aug 2017 Category: C Views: 143

Edited on 10 Aug 2017



Sum of first 10 integers using for loop

Loops are used in programming to repeat a specific block of code. Let us discuss about one of the simple and important loop in C language with a sample program. 

The syntax of for loop in C programming language is given as below:       

for(init; condition; increment)
{
        statement(s);
}

       

       Here , 'init' step is executed first and only once.  This step is used to initialize the loop control variable. Then the 'condition'   is evaluated and if it is true then the body of the loop is executed and if it is false the control jumps to the next statement just just after the 'for' loop. After the body of the 'for' loop executes, the flow of control jumps back up to the 'increment' statement. This statement allows you to update any loop control variables.

This process repeats until the condition become false.

The  program below explains the working of the 'for loop'  in C language.

 

#include <stdio.h>
#include <conio.h>

void main()
{
    int i, sum=0; //variable declaration and here variable sum is assigned to zero to avoid the garbage value.
    for(i=0;i<=10;i++)
    {
        sum=sum+i;
    }
    printf(" the sum is %d", sum);
    getch();
}

 

      Here the condition will be true upto '10' . and when it reaches the value 11 the condition becomes false and loop terminates.

 



Leave a Comment:


Advertisement