The basics of programming in c++ for beginners

Tasks: while loops, do while, nested loops

We met with loops while and do while in C++ and c nested constructions in loops. Let us tasks solving.

1. Organize continuous input of numbers with the keyboard, until the user has entered 0. After entering a zero, show on the screen the number of numbers, which were introduced, their total amount and the arithmetic mean. Tip: you must declare the counter variable, that will count the number of entered numbers, and variable, that will accumulate a total sum of numbers.

2. It is necessary to sum up all the odd integers, which will introduce the user to the keyboard.

3. The task is more difficult. Draw an isosceles triangle of characters ^. The height of a user selects. For example: height = 5, on the screen

task nested loops

173 thoughts on “Tasks: while loops, do while, nested loops

  1. In the last task, if you solve the same way as the authors, then there will be bugs, i entered 111 and there the line was out of order krch

  2. not tasks, what the fuck- then, like solving problems on tskils, but in fact here from the cycles only the name, more precisely if it is more correct to express, understanding cycles will not help in solving these problems

  3. FIRST
    #include

    int main(void) {
    double val, sum = 0, amount = 0;
    std::cout <> val && val) {
    std::cout << "Val: " << val << '\n';
    ++amount;
    sum += val;
    }
    std::cout << "Total: " << sum << '\n';
    std::cout << "Amount: " << amount << '\n';
    }

    SECOND

    #include ;
    int main(void) {
    int begin, end;
    std::cout <> begin >> end && begin > end)
    {
    if (std::cin.fail()) {
    std::cout < end) {
    std::cout << "First value mast be less than second\n";
    }
    }
    std::cout << "begin: " << begin << ", end " << end << '\n';

    unsigned total = 0;
    for (int i = begin; i <= end; i ) {
    if (!(i % 2)) {
    std::cout << i << ' ';
    total += i;
    }
    }
    std::cout << "\nTotal: " << total << '\n';
    }

  4. Самое легкое решение первой задачи в минимум строк
    void main()
    {
    setlocale(LC_ALL, “ru”);

    int b;
    int a = 1;
    float suma = 0;
    int i = 0;
    for (;i < a;i )
    {
    a++;
    cout <> b;
    suma += b;
    if (b == 0)
    {
    break;
    }
    }
    cout << "Сумма:" << suma << endl;
    cout << "Средние арефм:" << double(suma / i);

    1. int b;
      int a = 1;
      float suma = 0;
      int i = 0;
      for (;i < a;i )
      {
      a++;
      cout <> b;
      suma += b;
      if (b == 0)
      {
      break;
      }
      }
      cout << "Сумма:" << suma << endl;
      cout << "Средние арефм:" << double(suma / i); P.S Плохо скопировал.

      1. аааа а это сайт удаляет лол а как тогда вставить)) , если он код убирает))

  5. 2 a task
    int s;
    int start_range;
    int close_range;

    cout <> start_range;
    cout <> close_range;

    for (int i = 0;i < close_range;i )
    {

    s=start_range % 2;
    if (s !=0)
    {

    cout << start_range << " ";
    }
    if (start_range == close_range)
    {
    break;
    }
    start_range++;

  6. 3 задача с помощью do while(как написано в заголовке):

    #include

    using namespace std;

    int main() {
    setlocale(LC_ALL, “RU”);

    int hight, hight1, amount1, amount = 1, str = 0;

    cout <> hight;

    hight1 = hight;
    cout << endl;

    do {
    amount1 = amount;

    do {
    cout < 0);

    do {
    cout < 0);

    cout << endl;

    str++;
    hight1 = hight – str;
    amount += 2;
    } while (str < hight);

    return 0;
    }

  7. я конечно не эксперт, но вышло как-то так)

    //task 1
    #include
    using namespace std;

    float general = 0;
    float sum = 0;
    float sr;
    float m;
    int main() {
    setlocale(LC_ALL, “RU”);

    for (int i = 0; ; i )
    {
    cout << i + 1 <> m;

    if (m) {
    sum += m;
    general++;
    }
    else
    {
    sr = sum / general;
    cout << "\nВсего чисел было: " << general << endl << "Общая сумма чисел: " << sum << endl;
    cout << "Среднее арифметическое: " << sr << endl;
    break;
    }
    }
    return 0;
    }

  8. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “rus”);

    int digit = 0;
    double amount_digit = 0;
    double sum_digit = 0;

    cout << "Введите числа для расчета.\n";
    cout << "0 – закончить ввод чисел.\n\n";
    for (amount_digit = 1; ; amount_digit++)
    {
    cout << "Введите " << amount_digit <> digit;
    sum_digit += digit;

    {
    if (digit)
    amount_digit;

    else if (digit == 0)
    {
    amount_digit–;
    break;
    }
    }
    }
    cout << "Было введено чисел – " << amount_digit << endl;
    cout << "Сумма всех чисел равна – " << sum_digit << endl;
    cout << "Среднее арфиметическое – " << sum_digit / amount_digit << endl;
    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *