The do-while loop
Syntax:
do{
statement to be executed ;
update-condition ;
}
while( test-condition );
Program to print the odd numbers up to 100 using do-while loop.
#include<iostream.h>
#include<conio.h>
void main() {
clrscr() ;
int i = 1 ;
do {
cout<<i ;
i=i+1 ;
}
while( i<100 ) ;
getch() ;
}
Program to print the even numbers using do-while loop and ranges are taken by user.
#include<iostream.h>
#include<conio.h>
void main() {
clrscr() ;
int i, starting , ending ;
cout<<”\n Enter the starting range of Even number series. ” ;
cin>>starting ;
cout<<”\n Enter the end point of Even number series. ” ;
cin>>ending ;
i = starting ;
do {
if ( i%2==0 )
{ cout<<”Even number = ”<<i ; }
i = i+1 ;
}
while ( i < ending ) ;
getch() ;
}
Program to identify Prime Number between 1 to 50 using while loop.
#include<iostream.h>
#include<conio.h>
void main() {
clrscr() ;
int number, counter , divisor ;
number=1 ;
do {
counter = 0 ;
divisor = number ;
while( divisor>0 )
{
if ( number % divisor ==0 )
{ counter++ ; }
else
{ continue ; }
divisor-- ;
}
if ( counter == 2 )
{ cout<<”\n Prime Number is = ”<< number ; }
number = number + 1 ;
}
while ( number < 50 ) ;
getch() ;
}
___________________________________________________________________________________
0 comments:
Post a Comment
Please give your valuable suggestions