for loop in C with example
for loop in C:
In the c programming or any other programming language for loop is used to repeat a block of code until the specified condition met or satisfied. for is a widely used loop in the programming language.
Syntax of for loop in C:
for (initializationStatement; testExpression; updateStatement){
// statements inside the body of loop}
if the condition lies between "initialisationStatement " and "testExpression" then code inside the for block executes otherwise dose not execute. and after every execution "updateStatement" update the statement.
Flow chart of for loop in C:
Example of for loop in C:
problem statement-1: In this problem statement we need to print 1 to 10 by using the for loop in c language.
// Print numbers from 1 to 10#include <stdio.h>
int main() { int i;
for (i = 1; i < 11; ++i) {
printf("%d ", i); }
return 0;}
Output:
1 2 3 4 5 6 7 8 9 10
problem statement-2: write the code in C by using for loop for print the given star formation.
Input:
5
Output:
*
**
***
****
*****
/* C Program to Print Right Angled Triangle Star Pattern */#include <stdio.h>int main(){int Rows, i, j;printf("Please Enter the Number of Rows: ");scanf("%d", &Rows);for ( i = 1 ; i <= Rows; i++ ){for ( j = 1 ; j <= i; j++ ){printf("* ");}printf("\n");}return 0;}
0 Comments