
In programming, decision making is a fundamental concept. Often you have to make choices based on certain conditions.
This is the moment when conditional statements, such as “if” and “else,” play a big role. These statements allow you to execute different blocks of code depending on whether a certain condition is true or false.
if ( condition ) {
statements_for_true_condition ;
}
else {
statements_for_false_condition ;
}You can also use multiple “if-else” statements and combine them to create complex decision structures as in the examples below.
Write a program to calculate the sum of all even two-digit numbers. The calculated sum is printed on the screen.
Solution:
#include <iostream>
using namespace std;
int main () {
int i = 10, sum = 0;
while (i <= 98) {
sum = sum + i;
i+=2;
}
cout << sum << endl;
return 0;
}Write a program to calculate the sum of all odd two-digit numbers. The program prints the sum on the screen in the following format:
11 + 13 + 15 + 17 + … + 97 + 99 = 2475
Solve the program without using the if statement
Solution 1:
#include <iostream>
using namespace std;
int main () {
int i = 11, sum = 0;
cout << i;
sum = i;
i=i+2;
while (i <= 99){
cout << " + " << i;
sum = sum + i;
i+=2;
}
cout << " = " << sum << endl;
return 0;
}Solution 2:
#include <iostream>
using namespace std;
int main () {
int i = 11, sum = 0;
while (i <= 97) {
cout << i << " + ";
sum = sum + i;
i+=2;
}
sum = sum + i;
cout << i << " = " << sum << endl;
return 0;
}Write a program to calculate \$y = x^n\\$ for a given natural number n, n >= 1 and real number x.
Solution 1:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int counter = 0, n;
float x, y = 1;
cout << "x: ";
cin >> x;
cout << "n: ";
cin >> n;
while (counter < n) {
y *= x;
counter++;
}
cout << x << "^" << n << " = " << y << endl;
return 0;
}Solution 2:
#include <iostream>
using namespace std;
int main() {
int counter = 0, n;
float x, y = 1;
cout << "x: ";
cin >> x;
cout << "n: ";
cin >> n;
do {
y *= x;
counter++;
} while (counter < n);
cout << x << "^" << n << " = " << y << endl;
return 0;
}Solution 3:
#include <iostream>
using namespace std;
int main() {
int counter, n;
float x, y;
cout << "x: ";
cin >> x;
cout << "n: ";
cin >> n;
for(counter = 1, y = x; counter < n; counter++) {
y *= x;
}
cout << x << "^" << n << " = " << y << endl;
return 0;
}Solution 4:
#include <iostream>
using namespace std;
int main() {
int counter = 0, n;
float x, y = 1;
cout << "x: ";
cin >> x;
cout << "n: ";
cin >> n;
for( ; counter < n; counter++) {
y *= x;
}
cout << x << "^" << n << " = " << y << endl;
return 0;
}Write a program that will determine the number of numbers divisible by 3, when divided by 3 have a remainder of 1, or 2, out of n numbers (entered from the keyboard).
Solve the task with while, do…while and
for
Solution with while:
#include <iostream>
using namespace std;
int main() {
int n = 1, i = 0, number, div, r1, r2;
div = r1 = r2 = 0; /* counters */
cin >> n; /* input numbers */
while (i < n) {
cin >> number;
if (number % 3 == 0)
div++;
else if (number % 3 == 1)
r1++;
else r2++;
i++;
}
cout << div << endl;
cout << r1 << endl;
cout << r2 << endl;
return 0;
}Решение со do… while:
#include <iostream>
using namespace std;
int main() {
int n = 1, i = 0, number, div, r1, r2;
div = r1 = r2 = 0; /* counters */
cin >> n; /* input numbers */
do {
cin >> number;
if (number % 3 == 0)
div++;
else if (number % 3 == 1)
r1++;
else r2++;
i++;
} while (i < n);
cout << div << endl;
cout << r1 << endl;
cout << r2 << endl;
return 0;
}Solution with for:
#include <iostream>
using namespace std;
int main() {
int n = 1, i = 0, number, div, r1, r2;
div = r1 = r2 = 0; /* counters */
cin >> n; /* input numbers */
for (i = 0; i < n; ++i) {
cin >> number;
if (number % 3 == 0)
div++;
else if (number % 3 == 1)
r1++;
else r2++;
}
cout << div << endl;
cout << r1 << endl;
cout << r2 << endl;
return 0;
}For three entered line segments, determine whether it is possible to construct a triangle and whether the triangle is right-angled, acute-angled, or obtuse-angled.
For a given center of a circle and its radius, determine which quadrants the circle passes through.