while loop in c programming
While loop.It is an iteration looping control statement which has 5 parts.
1.declration part
2.condition part
3.while body
4.increment/decrement part
5.end of the loop
In this loop the control will first check the condition if the condition is true then it jump to the body of the while and execute all the statements in a cyclic manner up to the condition being satisfied.
Syntax of while loop
datatype variable name;
while(condition)
{
statement.........1;
statement..........n;
inc/dec;
}
1.Write a program then print all the numbers from 1 to 10;
#include<stdio.h>
#include<conio.h>
main( )
{
int no=1;
while(no<=10)
{
printf("%d,",no);
no++;
}
getch();
}
output:-1,2,3,3,4,5,6,7,8,9,10
2.Write a program then print all the numbers from 10 to 1.
#include<stdio.h>
#include<conio.h>
main( )
{
int no=10;
while(no>=1)
{
printf("%d,",no);
no--;
}
getch();
}
output:-10,9,8,7,6,5,4,3,2,1
3.Write a program enter a range then print all the even numbers.
#include<stdio.h>
#include<conio.h>
main( )
{
int n,no=1;
printf("enter a range");
scanf(%d",&n);
while(no<=n)
{
if(no%2==0)
printf("%d,",no);
no++;
}
getch();
}
output:-
enter the range 10
2,4,6,8,10
4.Write a program enter a range then print all the odd numbers.
#include<stdio.h>
#include<conio.h>
main( )
{
int n,no=1;
printf("enter a range");
scanf(%d",&n);
while(no<=n)
{
if(no%2==1)
printf("%d,",no);
no++;
}
getch();
}
output:-
enter the range 10
1,3,5,7,9
No comments:
Post a Comment