Viser opslag med etiketten programming. Vis alle opslag
Viser opslag med etiketten programming. Vis alle opslag

søndag den 14. juli 2019

C++ exercise 1.9 - C++ Primer 4th edition 2005

C++ Primer Fourth Edition 2005
ISBN-13: 978-0-201-72148-5
ISBN:10: 0-201-72148-1
Page 17.
Exercise Section 1.4.2
Exercise 1.9: What does the following for loop do? What is the final value of sum?

int sum = 0;
for(int i = -100; i <= 100; ++i)
    sum += i;

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