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.
/*
* C++ Program to Demonstrate do-while loop
*/
#include <iostream>
int main()
{
int a, d, n;
std::cout << "Enter a, d, n of AP: ";
std::cin >> a >> d >> n;
std::cout << std::endl << "Terms of AP : a = " << a
<< ", d = " << d << ", n = " << n << std::endl;
do {
std::cout << a << "\t";
a = a + d;
}
while (n-- > 1);
std::cout << std::endl;
}
$ ./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.
Related Posts:
- Check Programming Books
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice Programming MCQs