for loop in C Language
In C programming language, for loops are the most complex loops. The for loop is similar to while loop and is used when programmer knows in advance how many times the script should run.
The syntax of a for loop is:
for (initialize counter; test condition; increment/decrement counter) {
//Statement(s) to be executed repeatedly
}
Parameters:
• initialize counter: Initialize the loop counter value once unconditionally at the beginning of the loop.
• test condition: In the beginning of each iteration, condition is evaluated. If it evaluates to true, the loop continues and the nested statement(s) are executed. If it evaluates to false, the execution of the loop ends.
• increment/decrement counter: At the end of each iteration, increases/decreases the loop counter value
// Program to Print Multiples of a Given Number #include<stdio.h> #include<conio.h> void main() { int i,n,sum=0; printf("Input number of terms : "); scanf("%d",&n); printf("\nThe odd numbers are :"); for(i=1;i<=n;i++) { printf("%d ",2*i-1); sum+=2*i-1; } printf("\nThe Sum of odd Natural Number upto %d terms : %d \n",n,sum); getch(); } Output : Input number of terms : 10 The odd numbers are :1 3 5 7 9 11 13 15 17 19 The Sum of odd Natural Number upto 10 terms : 100