Exercise 5
Write a program that will print all four-digit numbers where the sum of the three least significant digits is equal to the most significant digit.
4031 (4=0+3+1), 5131 (5=1+3+1)
Solution:
#include <iostream>
using namespace std;
int main() {
int i, n, sum, first_digit, digit;
= 1000;
i while (i <= 9999) {
= i / 1000;
first_digit = i % 1000;
n = 0;
sum while (n > 0) {
= n % 10;
digit += digit;
sum /= 10;
n }
if (sum == first_digit) cout << i << "\t";
++;
i}
return 0;
}
Write a program that will print all numbers from a given range that are read the same from left to right and from right to left.
example numbers
12321 5061605
Solution:
#include <iostream>
using namespace std;
int main() {
int i, from, to, temp, op, digit;
>> from >> to;
cin for (i = from; i <= to; i++) {
= i;
temp = 0;
op while (temp > 0) {
= temp % 10;
digit = op * 10 + digit;
op /= 10;
temp }
if (op == i) cout << i << "\t";
}
return 0;
}
Write a program that will determine the number with the maximum value from an unknown number of integers entered from the keyboard. The program ends if an invalid number representation is entered.
Solution:
#include <iostream>
using namespace std;
int main() {
int n, max;
if (cin >> max) {
while (cin >> n) {
if (max < n) {
= n;
max }
}
<< max;
cout } else {
<< "No number was entered.";
cout }
return 0;
}
Write a program that will determine the number with the maximum value from an unknown number of integers entered from the keyboard. In this case, numbers greater than 100 are not taken into account i.e. they are ignored. The program ends if an invalid number representation is entered.
Solution:
#include <iostream>
using namespace std;
int main() {
int n, max;
if (cin >> max) {
while (cin >> n) {
if (n > 100) continue;
if (max < n) {
= n;
max }
}
<< max;
cout } else {
<< "No number was entered.";
cout }
return 0;
}
Write a program that will determine the two numbers with the highest values from an unknown number of integers entered from the keyboard. The program ends if an invalid number representation is entered.
Example:
If the numbers 2 4 7 4 2 1 8 6 9 7 10 3 are entered, the program will print 10 and 9.
Solution:
#include <iostream>
using namespace std;
int main() {
int n, max1, max2, temp;
if (cin >> max1 >> max2) {
if (max2 > max1){
= max1;
temp = max2;
max1 = temp;
max2 }
while(cin >> n) {
if(n > max1){
= max1;
max2 = n;
max1 } else if (n > max2) {
= n;
max2 }
}
<< max1 << endl;
cout << max2 << endl;
cout } else {
<< "Enter at least 2 numbers";
cout }
return 0;
}
Write a program that will determine the difference between the sums of the numbers at even and odd positions (according to the order of entry) from N integers entered from the keyboard. If this difference is less than 10, the screen prints “The two sums are similar”, otherwise “The two sums differ greatly”.
Example:
For the numbers entered from the keyboard:
2 4 3 4 2 1 1 6 1 7
sum_odd_positions = 9
sum_even_positions = 22
The screen will print:
The two sums differ greatly
Solution:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int difference, i, n = 0, number = 0;
int sum_odd_positions = 0, sum_even_positions = 0;
>> n;
cin for (i = 1; i <= n; i++) {
>> number;
cin if (i % 2) {
+= number;
sum_odd_positions } else {
+= number;
sum_even_positions }
}
= sum_even_positions - sum_odd_positions;
difference if (abs(difference) < 10){
<< "The two sums are similar";
cout } else {
<< "The two sums differ greatly";
cout }
return 0;
}
Write a program that will determine the positions (ordinal numbers of entry) of the two consecutive numbers that have the highest sum from an unknown number of integers entered from the keyboard. The program ends if two negative integers are entered one after the other (consecutively).
Solution:
#include <iostream>
using namespace std;
int main() {
int pol_position, position, max_sum, sum, previous, next;
>> previous >> next;
cin = position = 2;
pol_position = sum = previous + next;
max_sum while (1) {
if (previous < 0 && next < 0) {
break;
}
= previous + next;
sum if (sum > max_sum) {
= sum;
max_sum = position;
pol_position }
= next;
previous >> next;
cin ++;
position}
if (position > 2)
<< "The numbers are at positions " << pol_position - 1 << " and " << pol_position << " and their sum is " << max_sum;
cout return 0;
}
In Morse code, the characters (letters, digits) are represented by dots and dashes. The digits from 0 to 4 are represented as follows: “—–,” “.—-,” “..—,” “…–,” “….-.” You enter a natural number N (N < 100,000) from the keyboard, and then N other natural numbers are entered. For each of the entered numbers, print the remainder when dividing that number by 5 in Morse code, each in a separate line.
Solution:
#include <iostream>
using namespace std;
int main() {
int n, current_number;
>> n;
cin
for (int i = 0; i < n; ++i) {
>> current_number;
cin int rest = current_number%5;
if (rest == 0) {
<<"-----"<<endl;
cout} else if (rest == 1) {
<<".----"<<endl;
cout} else if (rest == 2) {
<<"..---"<<endl;
cout} else if (rest == 3) {
<<"...--"<<endl;
cout} else {
<<"....-"<<endl;
cout}
}
return 0;
}
A “Rosary” is a “number” obtained by concatenating the first N numbers in order. For example, for N=4, it is 1234, and for N=11, it is 1234567891011. An “Reverse Rosary” is similar to the above, but it starts from N, and the numbers are concatenated in reverse order. So, for N=5, it is 54321.
For a given N, print in order on the screen: Rosaryfor 1, Reverse Rosary for 1, Rosary for 2, Reverse Rosary for 2, and so on, up to Reverse Rosary for N.
For example, for N=5, you should print:
1
1
12
21
123
321
1234
4321
12345
54321
Solution:
#include <iostream>
using namespace std;
int main() {
int i, j, N;
>> N;
cin
for (i = 1; i <= N; i++) {
for (j = 1; j <= i; j++) {
<< j;
cout }
<< endl;
cout for (j = i; j > 0; j--) {
<< j;
cout }
<< endl;
cout }
return 0;
}
The average number is a number where each subsequent digit is smaller than the previous one (when considering from the most significant digit to the least significant digit). For example, 7421. Single-digit numbers are not considered average numbers.
To write a program, first enter a number N, which determines the total number of numbers to be entered from the keyboard. Then, enter the N numbers one by one. As output, you should print all the numbers that meet the condition for an average number, and then print the minimum average number from the entered numbers.
If there is no number that satisfies the condition, the value -1 is printed.
Example input:
5
435
643
12234
721
7720
Example output:
643
721
643
Решение:
#include <iostream>
using namespace std;
int main() {
int num, i, tmp, min = -1, N;
>> N;
cin
for (i = 0; i < N; i++) {
>> num;
cin = num;
tmp while (tmp > 9) {
if (tmp % 10 >= (tmp / 10) % 10) break;
/= 10;
tmp }
if (tmp < 10) {
<< num << endl;
cout if (min == -1) min = num;
else if (num < min) min = num;
}
}
<< min;
cout return 0;
}
#include <iostream>
using namespace std;
int main() {
char answer;
<< "Is SP easy to learn? (y/n): ";
cout >> answer;
cin switch (answer) {
case 'Y':
case 'y':
<< "I think so too!" << endl;
cout break;
case 'N':
case 'n':
<< "Really?" << endl;
cout break;
default:
<< "Is this a yes or no question?" << endl;
cout }
return 0;
}
#include <iostream>
using namespace std;
int main() {
int value, choice;
<< "Enter the initial value: ";
cout >> value;
cin
do {
do {
<< "Menu:" << endl;
cout << "1 - Increase" << endl;
cout << "2 - Decrease" << endl;
cout << "3 - Double" << endl;
cout << "0 - Exit" << endl;
cout << "Choice: ";
cout >> choice;
cin } while (choice < 0 || choice > 3);
switch (choice) {
case 1:
++;
valuebreak;
case 2:
--;
valuebreak;
case 3:
*= 2;
value break;
case 0:
<< "End" << endl;
cout break;
default:
<< "Invalid choice!" << endl;
cout break;
}
<< "Value = " << value << endl;
cout } while (choice != 0);
return 0;
}
Write a program that will determine whether a character entered from the keyboard is a vowel, a digit or something else.
#include <iostream>
using namespace std;
int main() {
char znak;
<< "Enter a character: ";
cout >> znak;
cin
switch (znak) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
<< "The entered character is a vowel: " << znak << endl;
cout break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
<< "The entered character is a digit: " << znak << endl;
cout break;
default:
<< "The entered character is: " << znak << endl;
cout }
return 0;
}
Write a program that represents a simple calculator. The program reads two numbers and an operator in the format:
operator num2 num1
After performing the operation, depending on the operator, the result should be printed in the following format:
operator num2 = result num1
#include <iostream>
using namespace std;
int main() {
double num1, num2, result;
char op;
<< "Enter two numbers and an operator (e.g., 5 + 3): ";
cout >> num1 >> op >> num2;
cin
switch (op) {
case '+':
= num1 + num2;
result break;
case '-':
= num1 - num2;
result break;
case '*':
= num1 * num2;
result break;
case '/':
if (num2 != 0) {
= num1 / num2;
result } else {
<< "Division by 0 is not allowed." << endl;
cout return 1;
}
break;
default:
<< "Unknown operator." << endl;
cout return 1;
}
<< num1 << ' ' << op << ' ' << num2 << " = " << result << endl;
cout
return 0;
}