lørdag den 25. maj 2019

C++ using while loop to countdown from 10 to 0

Solution for the book C++ Primer 4th edition 2005. Page 17. Exercise 1.11

Hello and welcome to this blog post today we are going to make a solution for the exercise 1.11 on page 17 in C++ primer fourth edition book from 2005.

Exercise 1.11:
Write a program using while loop to print the numbers from 10 to down to 0.
Now rewrite the program using for loop.

While loop - Solution

#include <iostream> 
int main()
{
 int i = 11;
 int counter = 0;
 while (i > counter)
 {
  i--;
  std::cout << i << std::endl;
 }
 return 0;
}

For loop - Solution

#include <iostream>
int main()
{
 int counter = 0;

 for (int i = 10; i >= counter; i--)
 {
  std::cout << i << std::endl;
 }
 return 0;
}

Ingen kommentarer:

Send en kommentar