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.

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

  1. #include
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Rus”);
    int a, b,c,d;
    cout << "Введите два значения для перемножения:" << "\n";
    cout <> a;
    cout <> b;
    cout <> d;
    c = a * b;
    if (c==d)cout << "Ответ правильный!";
    else cout << "Ответ не верный!"<<endl;
    if(c != d)cout << "Верный ответ:"<<c<<endl;
    }

  2. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “RU”);
    float num1 = 0, num2 = 0;
    double result = 0, answer = 0;

    cout <> num1;
    cout <> num2;

    result = num1 * num2;

    cout << "Как вы думаете какой результат умножения " << num1 << " * " << num2 <> answer; cout << endl;

    if (answer == result)
    {
    cout << "Ваш ответ полностью верен!\n";
    }
    else if (answer != result)
    {
    cout << "Ваш ответ не верен!" << endl
    << num1 << " * " << num2 << " = " << result << endl;
    }

    return 0;
    }

  3. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “rus”);
    int a,b,c,d;
    cout << "Введите два натуральных однозначных числа" <> a >> b;
    if (a 9 || b 9)
    {
    cout << "Не натуральные или однозначные числа"<< endl;
    return 0;
    }

    c = a*b;
    cout <<endl<< "Введите ответ умноения " << a << " on " << b <> d;
    if (c == d)
    {
    cout <<endl << "Правильно! Вы ввели: " << d << endl << "Ответ: " << c << endl;
    }
    else
    {
    cout << "Не правильно. Вы ввели: " << d << endl << "Ответ: " << c;
    }
    return 0;
    }

  4. #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    int a=0,t1=0,t2=0,t3=0,t4=0;
    cout <> a;

    if (a < 0)
    {
    cout < 10000)
    {
    cout << "У тебя нет столько денег!\n";
    }
    else if (a == 10000)
    {
    cout < 0 && a < 10000)
    {
    t4 = a % 10;
    //cout << t4 << endl;
    a /= 10;
    t3 = a % 10;
    //cout << t3 << endl;
    a /= 10;
    t2 = a % 10;
    //cout << t2 << endl;
    a /= 10;
    t1 = a % 10;
    //cout << t1 << endl;
    cout < 0)
    {
    if (t1 == 1)
    cout << "Одну тысячу ";
    else if (t1 == 2)
    cout << "Две тысячи ";
    else if (t1 == 3)
    cout << "Три тысячи ";
    else if (t1 == 4)
    cout << "Четыре тысячи ";
    else if (t1 == 5)
    cout << "Пять тысяч ";
    else if (t1 == 6)
    cout << "Шесть тысяч ";
    else if (t1 == 7)
    cout << "Семь тысяч ";
    else if (t1 == 8)
    cout << "Восемь тысяч ";
    else if (t1 == 9)
    cout < 0)
    {
    if (t2 == 1)
    cout << "сто ";
    else if (t2 == 2)
    cout << "двести ";
    else if (t2 == 3)
    cout << "тристо ";
    else if (t2 == 4)
    cout << "четыресто ";
    else if (t2 == 5)
    cout << "пятьсот ";
    else if (t2 == 6)
    cout << "шестьсот ";
    else if (t2 == 7)
    cout << "семьсот ";
    else if (t2 == 8)
    cout << "восемьсот ";
    else if (t2 == 9)
    cout <0)
    {
    if (t3 == 2)
    cout << "двадцать ";
    else if (t3 == 3)
    cout << "тридцать ";
    else if (t3 == 4)
    cout << "сорок ";
    else if (t3 == 5)
    cout << "пятьдесят ";
    else if (t3 == 6)
    cout << "шестьдесят ";
    else if (t3 == 7)
    cout << "семьдесят ";
    else if (t3 == 8)
    cout << "восемьдесят ";
    else if (t3 == 9)
    cout << "девяносто ";
    }
    if (t3 == 1)
    {
    if (t4 == 0)
    cout << "десять рублей. ";
    else if (t4 == 1)
    cout << "одинадцать рублей. ";
    else if (t4 == 2)
    cout << "двенадцать рублей. ";
    else if (t4 == 3)
    cout << "тринадцать рублей. ";
    else if (t4 == 4)
    cout << "четырнадцать рублей. ";
    else if (t4 == 5)
    cout << "пятнадцать рублей. ";
    else if (t4 == 6)
    cout << "шестнадцать рублей. ";
    else if (t4 == 7)
    cout << "семнадцать рублей. ";
    else if (t4 == 8)
    cout << "восемнадцать рублей. ";
    else if (t4 == 9)
    cout < 0)
    {
    if (t4 == 0)
    cout << "рублей. ";
    else if (t4 == 1)
    cout << "один рубль. ";
    else if (t4 == 2)
    cout << "два рубля. ";
    else if (t4 == 3)
    cout << "три рубля. ";
    else if (t4 == 4)
    cout << "четыре рубля. ";
    else if (t4 == 5)
    cout << "пять рублей. ";
    else if (t4 == 6)
    cout << "шесть рублей. ";
    else if (t4 == 7)
    cout << "семь рублей. ";
    else if (t4 == 8)
    cout << "восемь рублей. ";
    else if (t4 == 9)
    cout << "десять рублей. ";
    }
    }
    return 0;
    }

  5. #include
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    int a=10,b=10,c=0;
    a++;
    b++;
    srand(time(0));

    a = 2 + rand() % 8;
    b = 2 + rand() % 8;
    cout << "Проверим твои способности\n";
    cout <<"Сколько будет "<< a << " * " << b<> c;
    if (c == a * b)
    cout << "Правильно!\n";
    else
    cout << "Не правильно! Answer: " << a * b << endl;
    return 0;
    }
    made it a little more interesting

  6. #include
    using namespace std;
    int main ()
    {
    int a=0;
    cin >> a;
    int b=0;
    cin >> b;
    you ansus=0;
    int corans=a*b;

    if (corans>=81 || corans<=0)
    {
    cout << "\nОшибка\n";
    return 0; // Added return statement to terminate the program immediately after an error message is displayed
    }

    if (ansus==corans)
    {
    cout << "Верно";
    }
    else
    {
    cout << "Неверно. Correct answer:" << corans << endl;
    }

    return 0;
    }

    1. Updated code: #include
      using namespace std;
      int main ()
      {
      int a=0;
      cin >> a;
      int b=0;
      cin >> b;
      you ansus=0;
      int corans=a*b;
      if (ansus==corans)
      {
      cout << "Верно";
      }
      else
      {
      cout << "Неверно. Correct answer:" << corans <=81 || corans<=0)
      {
      cout << "\nОшибка\n";
      return 0; // Added return statement to terminate the program immediately after an error message is displayed
      } was unnecessary

  7. #include
    #include

    int main()
    {
    int more;
    int a, b;
    int ans;
    link: std::cout << "Enter two integer values: \n";
    std::cout <> a;
    std::cout <> b;

    if ((a >= 0 || b >= 0) && a < 10 && b < 10)
    {
    much = a * b;
    std::cout << "Give an answer to the expression: " << a << " * " << b <> ans;
    if (years == mult)
    std::cout << "Congratulations! the answer " << much << " is correct!";
    else
    {
    std::cout << " Unfortunatly the answer " << ans << " is not correct!:-("
    "\n the right answer is " << much << "!";
    }
    }
    else
    {
    std::cout << "Try to enter a value 1 to 9!";
    goto link;
    }
    }

  8. int number1, number2, sum, sum1;
    cout <> number1;
    cout <> number2;
    cout <> sum;
    sum1 = number1 * number2;
    if (sum != sum1) {
    cout << "Ответ неверный, succeed: " << sum1;
    }
    else if (sum == sum1) {
    cout << "Ответ верный! Конечно же получится: " << sum1;
    }

Leave a Reply

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