While loop is a condition control loop.The while loop is used to carry out looping operation.In while loop,a statement or a group of statements is/are executed repetitively until some condition has been satisfied.There must be at least one such statement within the body of loop which changes the looping condition during the execution of loop,otherwise an infinite loop may occur.While loop is also called "Entry-Control Loop"because in while loop first the condition is checked and then the statement get executed.
while(condition)
statement;
or,
while(condition)
{statement-1;
statement-2;
-
-
statement-n;
}
Example-:
Write a program to find sum of series of terms: 1+3+5+ - - -n.
#include<stdio.h>
#include<conio.h>
main()
{ int term=0,i=1,s=0;
printf("Enter the number of term");
scanf("%d ",&term);
while(i<=term)
{s=s+(2*i-1);
i++;
}
printf("\n sum=%d",s);
getch();
}
Output-:Enter the number of term 2
sum=4
ads
share it
ReplyDelete