mandag den 27. maj 2019

C++ using while loop to sum the numbers from 50 to 100

This exercise is from the C++ primer 4th edition book from 2005. The program works and runs fine. This is a solution to the exersice section 1.4.2 on page 17.

Exercise 1.10: Write a program that uses for loop to sum the numbers from 50 to 100. Now rewrite the program using a while loop.

In this part we rewrite the for loop to using a while loop

søndag den 26. maj 2019

C++ using for loop to sum the numbers from 50 to 100

This exercise is from the C++ primer 4th edition book from 2005. The program works and runs fine. This is a solution to the exersice section 1.4.2 on page 17.

Exercise 1.10: Write a program that uses for loop to sum the numbers from 50 to 100. Now rewrite the program using a while loop.
💥 Beware: This is not just a simple program that adds two numbers together. It is a little bit more complicated than that.

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;
}