While Loop in C Programming & Examples
1].while Loop
The simplest
looping structure in C language is while loop. It is Entry Controlled loop
statement.
Syntax:-
Loop Variable=Value;
while (condition)
{
Statement 1;
.
. . . . . . . .
. . . . . . . . .
Statement N;
}
1. The
condition returns boolean value either true or false. If the given condition
return true then the body of loop is execute. After the execution of loop body
it again tests the given condition and if it is true it again execute the body
of loop. This process repeat until the condition remains true.
2. When
the condition returns false, the control is transferred from out of the loop.
The program will continue with the statements which are after the while loop.
3. The
body of loop contains one or more statements. The braces are required only if
the body contains the two or more statements.
WAP which print numbers from 1 to 10
#include<stdio.h>
#include<conio.h>
void main()
{
int
i;
clrscr();
i=1;
while(i<=10)
{
printf("%i\t",i);
i++;
}
getch();
}
WAP which print numbers from 10 to 1.
#include<stdio.h>
#include<conio.h>
void main()
{
int
i;
clrscr();
i=10;
while(i>=1)
{
printf("%i\t",i);
i--;
}
getch();
}
WAP
which print the following series
1 4 9 16 25 36 47 64 81 100
#include<stdio.h>
#include<conio.h>
void main()
{
int
i;
clrscr();
i=1;
while(i<=10)
{
printf("%i\t",i*i);
i++;
}
getch();
}
WAP which print all the odd numbers between the 1 to
20
#include<stdio.h>
#include<conio.h>
void main()
{
int
i;
clrscr();
i=1;
while(i<=19)
{
printf("%i\t",i);
i+=2;
}
getch();
}
WAP
which print the following series
A B C D E F G H I J
#include<stdio.h>
#include<conio.h>
void main()
{
int
i;
clrscr();
i=65;
while(i<=74)
{
printf("%c\t",i);
i++;
}
getch();
}
WAP
which print the following series
A b C d E f G h I j
#include<stdio.h>
#include<conio.h>
void main()
{
int
i;
clrscr();
i='A';
while(i<='J')
{
if(i%2==0)
{
printf("%c\t",i+32);
}
else
{
printf("%c\t",i);
}
i++;
}
getch();
}
Comments
Post a Comment