Written: 2022/03/17

Tags: C++, exercises, lecture 2, operators, flow control, functions

Summary

Solutions to the exercises proposed at the end of Lecture 2, held on March 18th, 2022.

Solutions

Exercise 1

Write a program that prompts the user to insert an integer value and sums its digits, printing them at the end (e.g. for a provided integer value of 76, the result should be 13).

Exercise 2

Write a program that includes a function that swaps the contents of two float variables, printing the two variables before and after function invocation (hint: function should return void).

Exercise 3

Implement the for loop, without using while or do while loops, only using if and goto statements.

Revisited solution to more accuretely depict for loop execution.