Skip to content

Instantly share code, notes, and snippets.

@Helen460
Last active March 21, 2025 15:07
Show Gist options
  • Save Helen460/8c00e86e981ef45a269f9fee491f1516 to your computer and use it in GitHub Desktop.
Save Helen460/8c00e86e981ef45a269f9fee491f1516 to your computer and use it in GitHub Desktop.
4DZ
// Завдання на один простий цикл while / do while / for
// 1. Обчислити суму чисел в діапазоні від 1 до 10 циклом.
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
SetConsoleOutputCP(1251);
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
cout << sum << "\n";
}
// 2. Програма виводить таблицю відповідності температур за Цельсієм і Фаренгейтом у вказаному діапазоні.
#include <iostream>
using namespace std;
int main() {
for (int celsius = 0; celsius <= 100; celsius++) {
int fahrenheit = (celsius * 9 / 5) + 32;
cout << celsius << "C - " << fahrenheit << "F\n";
}
}
// 4. Написати програму, яка обчислює факторіал введеного числа.
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
SetConsoleOutputCP(1251);
int n;
cout << "Введіть число: ";
cin >> n;
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
cout << factorial << "\n";
}
// 5. З клавіатури вводиться ціле число будь-якої розрядності. Визначити кількість цифр у ньому та їхню суму.
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
SetConsoleOutputCP(1251);
int num;
int sum = 0;
int count = 0;
cout << "Введіть число: ";
cin >> num;
while (num) {
sum += num % 10;
num /= 10;
count++;
}
cout << "Кількість цифр: " << count << "\n";
cout << "Сума цифр: " << sum << "\n";
}
// 6. З клавіатури вводиться ціле число.Вивести на екран всі числа, на які введене число ділиться без залишку.
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
SetConsoleOutputCP(1251);
int num;
cout << "Введіть число: ";
cin >> num;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
cout << i << "\n";
}
}
}
// Завдання на вкладені цикли:
// 1. Написати програму, що показує таблицю множення (таблиця Піфагора).
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
cout << i * j << "\t";
}
cout << "\n";
}
}
// 2. Показати номери та загальну кількість усіх щасливих трамвайних квитків з шестизначними номерами.
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
SetConsoleOutputCP(1251);
int count = 0;
for (int ticket = 0; ticket <= 999999; ticket++) {
int firsthalf = ticket / 1000;
int secondhalf = ticket % 1000;
if (((firsthalf / 100) + ((firsthalf / 10) % 10) + (firsthalf % 10)) ==
((secondhalf / 100) + ((secondhalf / 10) % 10) + (secondhalf % 10))) {
cout << "Щасливий квиток: " << ticket << "\n";
count++;
}
}
cout << "Загальна кількість щасливих квитків: " << count << "\n";
}
// Завдання на псевдографіку:
// 1. Показати на екрані рівнобедрений трикутник висотою N.
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
SetConsoleOutputCP(1251);
int N;
cout << "Введіть висоту трикутника: ";
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << "\n";
}
}
// 2. Показати на екрані каркас паралелепіпеда розмірністю AxBxC.
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
SetConsoleOutputCP(1251);
int A;
int B;
int C;
cout << "Введіть розміри паралелепіпеда (A, B, C): ";
cin >> A >> B >> C;
for (int z = 0; z < C; z++) {
for (int y = 0; y < B; y++) {
for (int x = 0; x < A; x++) {
if (x == 0 || x == A - 1 || y == 0 || y == B - 1 || z == 0 || z == C - 1) {
cout << "#";
}
else {
cout << " ";
}
}
cout << "\n";
}
cout << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment