C++ Basic Syntax Practice – do-while Loop (Part II)

Lesson 11: do-while Loop

B2068 Count 4-Digit Numbers That Meet Conditions

C++ Basic Syntax Practice - do-while Loop (Part II)

#include <iostream>
using namespace std;

int main() {
    int n, num, g, s, b, q, cnt = 0;
    cin >> n;
    int i = 1;
    do {
        cin >> num; // Input number
        g = num % 10; // Units
        s = num % 100 / 10; // Tens
        b = num / 100 % 10; // Hundreds
        q = num / 1000; // Thousands
        // If units - thousands - hundreds - tens is greater than 0, increment count
        if (g - q - b - s > 0) {
            cnt++;
        }
        i++;
    } while (i <= n);
    cout << cnt;
    return 0;
}

P1035 [NOIP 2002 Popular Group] Series Summation

C++ Basic Syntax Practice - do-while Loop (Part II)

#include <iostream>
using namespace std;

int main() {
    double k;
    cin >> k;
    double Sn = 0;
    double i = 0;
    do {
        i += 1;
        Sn += 1 / i;
    } while (Sn <= k);
    cout << i;
    return 0;
}

Leave a Comment