The basics of programming in c++ for beginners

Tasks: Select if and else statements in c ++

After reading the lesson aboutSelect if and else statements, go to practice. As usual, I ask you to try to solve all tasks independently, and only then look at proposed us decision.

1. The first task is interesting and fairly simple. But the code will succeed long. This task came up with our teachers of computer Academy. The idea was to – how to get beginning students suffer with writing code, to scribbling was more )))  The task: The user enters the number of the 1 to 9999 (the amount of the issuance of an ATM). It should display the words entered by the amount and in the end to write the name of the currency with the correct ending. For example: 7431 – seven thousand four hundred thirty-one Dollars_, 2149 – two thousand one hundred forty-nine dollars_, 15 – fifteen dollars_, 3 – three dollars_. To solve this task you will need to use the operator % (remainder of the division). Read about it in an article, you can Arithmetic operations in C ++  . Start!

2.  The user enters the serial number of the finger. It should show its name on the screen.

 3. Another challenge for yourself solutions. You must write a program, which checks the user on the knowledge of the multiplication table. The user enters two single-digit numbers. The program asks the question: the result of multiplying the first day of the second.  The user must enter a response and see on the screen is correct or not, he said,. If not– show yet and the correct result.

Those who can not cope – ask questions in the comments.

370 thoughts on “Tasks: Select if and else statements in c ++

  1. I did this the second time, but the first one didn’t work out…
    #include
    using namespace std;
    void main()
    {
    int i =0;
    cout <> i;
    switch (i)
    {
    case 1:
    cout << "thumb\n";
    break;
    case 2:
    cout << "forefinger\n";
    break;
    case 3:
    cout << "long finger\n";
    break;
    case 4:
    cout << "ring finger\n";
    break;
    case 5:
    cout << "little finger\n";
    break;
    default:
    cout << "Error\n";
    break;

    }
    }

  2. Last task
    #include
    using namespace std;
    void main()
    {
    setlocale(LC_ALL, "Rus");
    int i = 0, j = 0, rezult = 0, sum = 0;
    cout <> i >> j;
    sum = i * j;
    cout <> rezult;
    if (sum == rezult)
    cout << "Поздравляем ответ правильный!\n";
    else
    cout << "\nУчите таблицу умножения!!!\n";

    }

  3. My version of isolating numbers from numbers, entered by the user in the first task. No division, no leftovers.

    int summ;
    cout <> summ;
    int num[6]{0, 0, 0, 0, 0, 0};
    int minus[6]{100000, 10000, 1000, 100, 10, 1};
    summ++;
    for (int i = 0; i 0)
    {
    summ = summ - minus[i];
    num[i]++;
    }
    if (num[i] > 0)
    {
    num[i]--;
    summ = summ + minus[i];
    }
    }

    I also scored phrases through arrays:

    string hungreds[9]{"Сто ", "Двести ", "Триста ", "Четыреста ", "Пятьсот ", "Шестьсот ", "Семьсот ", "Восемьсот ", "Девятьсот "};
    string second_ten[9]{"одиннадцать ", "двенадцать ", "тринадцать ", "четырнадцать ", "пятнадцать ", "шестнадцать ", "семнадцать ", "восемнадцать ", "девятнадцать "};
    string tens[9]{"десять ", "двадцать ", "тридцать ", "сорок ", "пятьдесят ", "шестьдесят ", "семьдесят ", "восемьдесят ", "девяносто "};
    string nums[9]{"одна ", "две ", "три ", "четыре ", "пять ", "шесть ", "семь ", "восемь ", "девять "};
    string numss[9]{"один ", "два ", "три ", "четыре ", "пять ", "шесть ", "семь ", "восемь ", "девять "};
    if (num[0] != 0 || num[1] != 0 || num[2] != 0)
    {
    if (num[0] != 0)
    cout << hungreds[num[0] - 1];
    if (num[1] == 1 && num[2] != 0)
    cout << second_ten[num[2] - 1];
    else
    {
    if (num[1] != 0)
    cout << tens[num[1] - 1];
    if (num[2] != 0)
    cout << nums[num[2] - 1];
    }
    if (num[2] == 1 && num[1] != 1)
    cout << "тысяча ";
    else if ((num[2] == 2 || num[2] == 3 || num[2] == 4) && num[1] != 1)
    cout << "тысячи ";
    else cout << "тысяч ";
    }
    if (num[3] != 0)
    cout << hungreds[num[3] - 1];
    if (num[4] == 1 && num[5] != 0)
    cout << second_ten[num[5] - 1];
    else
    {
    if (num[4] != 0)
    cout << tens[num[4] - 1];
    if (num[5] != 0)
    cout << numss[num[5] - 1];
    }
    if (num[5] == 1 && num[4] != 1)
    cout << "рубль." << endl;
    else if ((num[5] == 2 || num[5] == 3 || num[5] == 4) && num[4] != 1)
    cout << "рубля." << endl;
    else cout << "рублей." << endl;

  4. #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");
    int object = 0 ;
    int o1000 = 0 ;
    int o100 = 0 ;
    int o10 = 0 ;
    int o1 = 0 ;
    int isc = 0; //исключения(одиннадцать, пятнадцать и т.д.)
    cout <> object;

    o1000 = object / 1000;
    o100 = (object / 100) % 10;
    o10 = (object / 10) % 10;
    o1 = object % 10;
    isc = object % 100;

    if (o1000 == 1)
    cout << " Тысяча ";

    if (o1000 == 2)
    cout << " Две тысячи";

    if (o1000 == 3)
    cout << " Три тысячи ";

    if (o1000 == 4)
    cout << " Четыре тысячи ";

    if (o1000 == 5)
    cout << " Пять тысяч ";

    if (o1000 == 6)
    cout << " Шесть тысяч ";

    if (o1000 == 7)
    cout << " Семь тысяч ";

    if (o1000 == 8)
    cout << " Восемь тысяч ";

    if (o1000 == 9)
    cout << " Девять тысяч ";

    if (o100 == 1)
    cout << " сто ";

    if (o100 == 2)
    cout << " двести ";

    if (o100 == 3)
    cout << " триста ";

    if (o100 == 4)
    cout << " четыриста ";

    if (o100 == 5)
    cout << " пятьсот ";

    if (o100 == 6)
    cout << " шестьсот ";

    if (o100 == 7)
    cout << " семьсот ";

    if (o100 == 8)
    cout << " восемьсот ";

    if (o100 == 9)
    cout << " девятьсот ";

    if (isc == 11)
    cout << " одиннадцать рублей. \n";

    if (isc == 12)
    cout << " двенадцать рублей. \n";

    if (isc == 13)
    cout << " тринадцать рублей. \n";

    if (isc == 14)
    cout << " четырнадцать рублей. \n ";

    if (isc == 15)
    cout << " пятнадцать рублей. \n";

    if (isc == 16)
    cout << " шестнадцать рублей. \n";

    if (isc == 17)
    cout << " семнадцать рублей. \n";

    if (isc == 18)
    cout << " восемнадцать рублей. \n";

    if (isc == 19)
    cout << " девятнадцать рублей. \n";

    if (o10 == 1)
    cout << " десять рублей. \n ";

    if (o10 == 2)
    cout << " двадцать ";

    if (o10 == 3)
    cout << " тридцать ";

    if (o10 == 4)
    cout << " сорок ";

    if (o10 == 5)
    cout << " пятьдесят ";

    if (o10 == 6)
    cout << " шестьдесят ";

    if (o10 == 7)
    cout << " семьдесят ";

    if (o10 == 8)
    cout << " восемьдесят ";

    if (o10 == 9)
    cout << " девяносто ";

    if ((o1 == 1) && (o10 != 1))
    cout << " один рубль \n ";

    if ((o1 == 2) && (o10 != 1))
    cout << " два рубля. \n ";

    if ((o1 == 3) && (o10 != 1))
    cout << " три рубля. \n ";

    if ((o1 == 4) && (o10 != 1))
    cout << " четыре рубля. \n ";

    if ((o1 == 5) && (o10 != 1))
    cout << " пять рублей. \n ";

    if ((o1 == 6) && (o10 != 1))
    cout << " шесть рублей. \n ";

    if ((o1 == 7) && (o10 != 1))
    cout << " семь рублей. \n ";

    if ((o1 == 8) && (o10 != 1))
    cout << " восемь рублей. \n";

    if ((o1 == 9) && (o10 != 1))
    cout << " девять рублей. \n ";

    if ((o1 == 0) && (o10 != 1))
    cout << " рублей. \n ";

    _getch();
    return 0;

    }

    like this somehow

  5. task №3.
    True, it was not possible to set conditions on variables a and b.
    #include
    using namespace std;

    int main()
    {
    setlocale(0, "");
    int a, b, p, np = 0;

    cout << "\t\t\tПроверка таблицы умножения\nВы должны задать 2 числа и написать сумму произведения этих чисел\n\n\n";
    cout << a;
    cout << b;
    cout << "Введите ответ на уравнение: " << a << " * " << b << np;

    cout << endl;
    p = a * b;

    if ((a,b) <=9)
    {
    if (p != np) cout << "НЕ ПРАВИЛЬНО!\nСоветую выучить таблицу умножения!!!\n\n";
    else if (p == np) cout << "ПРАВИЛЬНО!\n\n";
    }
    else cout << "Неверное число!";
    return 0;
    }

  6. Good day! Here is my solution to task No. 1. True, not everything is done through the if statement. If there are any shortcomings or suggestions, unsubscribe, you are welcome.

    #include
    #include
    #include

    using namespace std;

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

    int full_num , first_num , second_num , third_num , fourth_num , currency_num, cntrl = 0;

    cout << "Введите ваше число: " <> full_num;
    cout << "Выберите валюту для вывода: " << endl;
    cout << "1. Dollars" << endl;
    cout << "2. Euro" << endl;
    cout << "3. Hryvnia" <> currency_num;
    cout << endl;
    fourth_num = full_num % 10;
    full_num /= 10;
    third_num = full_num % 10;
    full_num /= 10;
    second_num = full_num % 10;
    full_num /= 10;
    first_num = full_num;

    switch (first_num)
    {
    case 0: {cout << "\0";break;}
    case 1: {cout << "Одна тысяча ";break;}
    case 2: {cout << "Две тысячи ";break;}
    case 3: {cout << "Три тысячи ";break;}
    case 4: {cout << "Четыре тысячи ";break;}
    case 5: {cout << "Пять тысяч ";break;}
    case 6: {cout << "Шесть тысяч ";break;}
    case 7: {cout << "Семь тысяч ";break;}
    case 8: {cout << "Восемь тысяч ";break;}
    case 9: {cout << "Девять тысяч ";break;}
    }

    switch (second_num)
    {
    case 0 : {cout << "\0"; break;}
    case 1 : {cout << "сто "; break;}
    case 2: {cout << "двести "; break;}
    case 3: {cout << "стриста "; break;}
    case 4: {cout << "четыреста "; break;}
    case 5: {cout << "пятсот "; break;}
    case 6: {cout << "шестьсот "; break;}
    case 7: {cout << "семьсот "; break;}
    case 8: {cout << "восемьсот "; break;}
    case 9: {cout << "девятсот "; break;}
    }

    switch (third_num)
    {
    case 0 : {cout << "\0"; break;}
    case 1 : {if (fourth_num == 0) cout << "десять "; else cout << "\0"; break;}
    case 2 : {cout << "двадцать "; break;}
    case 3 : {cout << "тридцать "; break;}
    case 4 : {cout << "сорок "; break;}
    case 5 : {cout << "пятьдесят "; break;}
    case 6 : {cout << "шестьдесять "; break;}
    case 7 : {cout << "семьдесят "; break;}
    case 8 : {cout << "восемьдесят "; break;}
    case 9 : {cout < 1) && fourth_num != 0)
    {
    switch (fourth_num)
    {
    case 1 : {cout << "один "; break;}
    case 2 : {cout << "два "; break;}
    case 3 : {cout << "три "; break;}
    case 4 : {cout << "четыре "; break;}
    case 5 : {cout << "пять "; break;}
    case 6 : {cout << "шесть "; break;}
    case 7 : {cout << "семь "; break;}
    case 8 : {cout << "восемь "; break;}
    case 9 : {cout << "девять "; break;}
    }
    }
    else if (third_num == 1 && fourth_num != 0)
    {
    switch (fourth_num)
    {
    case 1: {cout << "одиннадцать "; break;}
    case 2: {cout << "двенадцать "; break;}
    case 3: {cout << "тринадцать "; break;}
    case 4: {cout << "четырнадцать "; break;}
    case 5: {cout << "пятнадцать "; break;}
    case 6: {cout << "шестнадцат "; break;}
    case 7: {cout << "семнадцать "; break;}
    case 8: {cout << "восемнадцать "; break;}
    case 9: {cout << "девятнадцать "; break;}
    }
    }

    if (currency_num == 1)
    {
    if (fourth_num == 1 && third_num != 1) cout << "доллар" << endl;
    else if (((fourth_num == 2 || fourth_num == 4) || fourth_num == 3) && third_num != 1) cout << "доллара" << endl;
    else cout << "долларов" << endl;
    }
    else if (currency_num == 2) cout << "евро" << endl;
    else
    {
    if (fourth_num == 1 && third_num != 1) cout << "гривна" << endl;
    else if ((((fourth_num == 2 || fourth_num == 4) || fourth_num == 3) && third_num != 1) && cntrl != 1) cout << "гривны" << endl;
    else cout << "гривен" << endl;
    }
    return 0;
    }

    1. I recommend displaying all switches() into the function Make the suffix as parameters of the function, serial number and prefix, which will be assigned to the root of the word as necessary, depending on the bit depth of the number. Then you get one switch() instead of 4

  7. The recommendations function is clear. But the point is, that I was trying to write a program using only selection statements. And if you do it completely “Beautiful”, then you can use functions and loops. Then the code will be reduced significantly.

  8. I don't think anyone will answer me, but I'll ask a question anyway :)
    Why doesn't the solution to problem 3 work? ? I've already checked everything several times.:
    #include
    using namespace std;
    int main() {
    setlocale(LC_ALL, “rus”);
    int a = 0, b = 0, c = 0, d = 0;
    cout <> a;
    cin >> b;
    c = a * b;
    cout <> d;
    if (c = d) {
    cout << "Ответ верный\n";
    }
    else
    cout << "Ответ не верный. The correct answer is: \n" << c;
    system("PAUSE");
    return 0;
    }
    Something like this.

    1. There are a lot of mistakes )))
      #include
      using namespace std;

      int main() {
      setlocale(LC_ALL, "rus");

      int a = 0, b = 0, c = 0, d = 0;
      cout < < a; // почему cout а не cin ? cin >> b;
      c = a * b;
      cout < < d; // почему cout а не cin ? if (c = d) { // почему = а не == ? (тут ты присвоил переменной с значение d) cout << "Ответ верный\n"; } else cout << "Ответ не верный. Правильным ответом является: \n" << c; system("PAUSE"); return 0; }

Leave a Reply

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