Do While Loop Program in C++

This C++ program demonstrates the use of do-while loop. The do-while loop works the same way as a while or for loop with a difference that the condition for execution of the next iteration is checked after the execution of statements inside the block.

Here is the source code of the C++ program demonstrates the use of do-while loop. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Demonstrate do-while loop
  3.  */
  4. #include <iostream>
  5.  
  6. int main()
  7. {
  8.     int a, d, n;
  9.  
  10.     std::cout << "Enter a, d, n of AP: ";
  11.     std::cin >> a >> d >> n;
  12.     std::cout << std::endl << "Terms of AP : a = " << a
  13.               <<  ", d = " << d << ", n = " << n << std::endl;
  14.     do {
  15.         std::cout << a << "\t";
  16.         a = a + d;
  17.     }
  18.     while (n-- > 1);
  19.     std::cout << std::endl;
  20. }

$ ./a.out
Enter a, d, n of AP: 2 2 10
Terms of AP : a = 2, d = 2, n = 10
2       4       6       8       10      12      14      16      18      20

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.