do-while loop example in C++

Printing only even number using 

#include<iostream.h>
#include<conio.h>

 void main()
  { 
    clrscr();
    int num,even=0;

     cout<<"Enter Number of limit: ";
     cin>>num;
    
// loop execute at  least one time doesn't matter condition true or false

   do
     {
        cout<<even<<" ";  // print even value
        even = even+2; // increment value by 2
     }
    while(even<num); // check condition

   getch();
 }

OUTPUT

Enter Number of limit: 9
0 2 4 6 8

a simple menu demonstration of two number operation using switch and do – while loop

#include<iostream.h>
#include<conio.h>

void main()
 {
  int ch;
  float num1,num2;

// creating menu wizard using do-while loop
  do{

  clrscr();
  cout<<"Enter any two number: ";
  cin>>num1>>num2;

  cout<<"\nselection operation\n";
  cout<<"1> Addition"<<endl;
  cout<<"2> Substriction"<<endl;
  cout<<"3> Multiply"<<endl;
  cout<<"4> Division"<<endl;
  cout<<"0> Exit"<<endl;

  cout<<"Your choice:";
  cin>>ch;

  cout<<endl;

   switch(ch)
  {
    case 1:    cout<<"\nAddition are"<<endl;
               cout<<num1<<"+"<<num2<<" = "<<num1+num2;
               getch();
               break;

   case 2:      cout<<"\nsubstriction are"<<endl;
               cout<<num1<<"-"<<num2<<" = "<<num1-num2;
               getch();
               break;

   case 3:      cout<<"\nmultiply are"<<endl;
               cout<<num1<<"x"<<num2<<" = "<<num1*num2;
               getch();
               break;

   case 4:      cout<<"\ndivision are"<<endl;
               cout<<num1<<"/"<<num2<<" = "<<num1/num2;
               getch();
               break;
  }
}while(ch!=0);
}
OUTPUT:-
Enter any two number: 12 13
selection operation
1> Addition
2> Substriction
3> Multiply
4> Division
0> Exit

Your choice:

multiply are
12x13 = 156

selection operation
1> Addition
2> Substriction
3> Multiply
4> Division
0> Exit
Your choice:0

Related exercise,

 

Leave a Reply

Your email address will not be published.