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

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

  1. Calculate and display in the form of a table the values ​​of the function F in the interval from Xinit. to Hkon. with step dX.
    F = ax ^ – bx •- with
    at X 3 and b = 0
    in other cases
    where a, B, c - real numbers.
    Function F must be valid, if the expression Structured Programming is NOT(Ac OR Vc) And (Ac MOD2 Sc) not equal to zero, and integer otherwise. Through Ac, Vc and Cc denote the integer parts of the values ​​a, B, with, operations NOT, And, OR and MOD2 (modulo addition 2) - bitwise. The values ​​of a, B, with, Hnach., Hkon., dX enter from keyboard

  2. #include
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Russian”);
    int sum,piston,a;
    piston = 0;
    sum = 0;
    cout << "Нажмите любую клавишу\n " << "…………….\n";
    while (_getch() != 48) {
    cout < “;
    cin >> a;
    piston ++;
    sum = sum + a;
    cout << "Чтобы продолжить нажмите любую клавишу \n " << "Чтобы завершить нажмите '0'\n";
    }
    cout << "Количество чисел = " << piston << endl << "Сумма = " << sum << endl;
    system("PAUSE");
    return 0;
    }

  3. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “RU”);
    int countNumber = 0;
    int sumNumber = 0;
    while (true)
    {
    int userNumber;
    cout << "Введите число : " <> userNumber;
    if (userNumber == 0) {
    cout << "Общее количество чисел : " << countNumber << endl
    << "Сумма всех чисел : " << sumNumber << endl
    << "Среднее арифметическое : " << sumNumber / countNumber << endl;
    break;
    }
    countNumber++;
    sumNumber = sumNumber + userNumber;
    }
    }

  4. #include
    using namespace std;

    void main()
    {
    setlocale(LC_ALL, “ru”);
    cout <> h;
    int space = h – 1;
    int block = 1;
    for (int i = 1; i <= h; i )
    {
    for (int j = 1; j <= space; j )
    cout << " ";
    for (int j = 1; j <=block; j )
    cout << "^";
    space–;
    block += 2;
    cout << endl;
    }
    }

  5. Given positive integers N and K. Using only addition and subtraction operations, find the quotient of dividing N by K, and also the remainder of this division

  6. Given an integer N (>1). If it is simple, that is, it has no positive divisors, Besides 1 and myself, then output True, otherwise output False

  7. int main()
    {
    int h = 0;
    char triangeBody[100] = “^”;
    cout <> h;
    int counterSpaces = 1;
    for (int i = 0; i = 1; j–) {
    cout << " ";
    }
    cout << triangeBody << endl;
    strcat_s(triangeBody, "^^");
    counterSpaces++;
    }
    }

  8. #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Rus”);
    int sum = 0, lot = 0, num = 1;
    cout << "Введите любое кол-во чисел чтобы узнать их общую сумму, среднее арифметическое и кол-во введенных чисел.\nВведите '0' to stop." << endl;
    for (;on one != 0;)
    {
    cout <> on one;
    on one != 0 ? lot++ : lot = lot;
    on one != 0 ? sum = sum + on one : sum = sum;
    }
    cout << "Общая сумма: " << sum << endl << "Среднее арифметическое: " << sum / lot << endl << "Кол-во введенных чисел: " << lot << endl;
    return 0;
    }

Leave a Reply

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