It is the most commonly used loop statement in C language. It consisting of three expressions.
Syntax :- for(initialize;condition;inc/dec)
{
Statements;
}
Flow Chart:-
Note:-
1. First it initialize the value of variable and then goes to condition.
2. If the condition is true it executes true statement and then goes to increment or decrements statement.
3. After increase or decrease the value and then goes to condition.
4. This process will be continued until the specified condition will be false.
Programs:-
#include<stdio.h>
#include<conio.h> void main() { int i; clrscr(); for(i=1;i<=10;i++) printf("%d\n",i); getch(); } |
Output:-
#include<stdio.h> #include<conio.h> void main() { int n,i; clrscr(); printf("enter n value: "); scanf("%d",&n); for(i=1;i<=n;i++) printf("%d\t",i); getch(); } |
Output:-
#include<stdio.h>
#include<conio.h> void main() { int n,i; clrscr(); printf("enter n value: "); scanf("%d",&n); for(i=n;i>=1;i--) printf("%d\t",i); getch(); } |
Output:-
* Write a program to print Sum of 1 to 10 Numbers.
#include<stdio.h>
#include<conio.h> void main() { int i,sum=0; clrscr(); for(i=1;i<=10;i++) { sum=sum+i; } printf(“The sum of integer from 0 to 10 is: %d”,sum); getch(); } |
Output:-
#include<stdio.h> #include<conio.h> void main() { int i,n; clrscr(); printf(“enter the no:”); scanf(“%d”,&n); for(i=1;i<=10;i++) { printf(“%d*%d=%d\n”,n,I,n*i); } getch(); } |
Output:-
______________________________________________________
______________________________________________________
_____________________________________________________
>> Back to C-Language Notes (Topic Wise)___________________________________________________
Post a Comment