It is another looping control statement which has also 5 parts.
1.Declaration part
2.Do while body
3.Increment/decrement part
4.End of the program
5.Condition part
In this loop the control will first enter in to the body of the do while then it jump into the condition part after checking the condition then it execute body statements in a cyclic manner up to the condition being satisfied.
Syntax of the do while
int no;//declaration part
do// keyword of do while loop
{
statement.....1;
----------------
----------------
statement......n;
inc/dec;
}
while(condition);
1.Write a program then print all the numbers from 1 to 10 using do while loop?
#include<stdio.h>
#include<conio.h>
void main( )
{
int no=1;
clrscr( );
do
{
printf("%d,",no);
no++;
}
while(no<=10);
getch();
}
2.Write a program then print all the odd numbers from 10 to 1 using do while loop?
#include<stdio.h>
#include<conio.h>
void main( )
{
int no=10;
clrscr( );
do
{
if(no%2!=0)
printf("%d,",no);
no--;
}
while(no>=1);
getch();
}
3.Write a program then print addition value of even numbers and multiplication value of odd numbers from 1 to 10 using do while loop?
#include<stdio.h>
#include<conio.h>
void main( )
int no=1,sum=0,mul=1;
{
clrscr( );
do
{
if(no%2==0)
{
S
sum=sum+no;
else
mul=mul*no;
no++;
}
while(no>=10);
printf("%d is the addition of even numbers", sum);
printf("%d is the multiplication of odd numbers",mul);
getch();
}
No comments:
Post a Comment