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. setlocale(LC_ALL, “rus”);
    int a, b, c,d;
    cin >> a >> b;
    cout <> d;
    if (c == d) cout << "true";
    else {
    cout << "false";
    }
    return 0;

  2. #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);

    int a,b;
    int c;
    int summ;
    cout <> a;
    cout <> b;
    summ = a * b;
    cout <> c;

    if (c == a*b) {
    cout << "Ваш ответ: " << c << " YOU WIN!!!";
    }
    else if (c != a * b) {
    cout << "Ваш ответ: " << c << " YOU LOSE!!!"<<endl;
    cout << "Правильный ответ: " << summ;
    }

    }

  3. int A;
    cout<>A;
    int B;
    cout<>B;
    int C;
    cout<>C;
    if(A*B==C){
    cout<<"Правильно!"<<C;
    }
    else{
    cout<<"Неверно, правильный ответ\t"<<A*B<<"\n";
    }
    return 0;

  4. #include
    #include
    using namespace std;
    // Works at random
    int main () {
    srand(time(NULL));
    int a, b;
    int answer;
    int attempAmount = 5; // Number of attempts

    attemp:
    for (int i = 0; i < attempAmount; i ) {
    a = rand() % 20; // a [0; 20]. You can change the random interval
    b = rand() % 20 – 10; // b [-10; 10]. You can change the random interval

    cout << a << " * " << b <> answer;
    if (a * b != answer) {
    cout << "Its wrong!" << endl
    << a << " * " << b << " = " << a * b << endl << endl;
    continue;
    goto attemp;
    } else {
    cout << "Its right, well done!" << endl << endl;
    continue;
    }
    }
    }

  5. I did 1 task it turned out on 45000 lines of code, only used If, else if and operator %, this is normal? Norm that 45.000 lines of code?
    But the program works well, everything displays as it should be.

  6. #include
    using namespace std;

    void main()
    {
    /*
    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 the correct result as well. * /

    setlocale(LC_ALL, “rus”);

    int num1 = 0, num2 = 0, result = 0;
    cout << "Проверка знаний таблицы умножения" << endl;
    cout <> num1;
    cout <> num2;

    cout <> result;

    if (result == num1 * num2) cout << "Вы дали верный ответ\n\n\n";
    else(result == !num1 * num2),cout << "ваш ответ не верный, correct answer answer:", cout << num1 * num2<<endl;

  7. In the comment above, something was not copied, here is the correct code:

    #include
    using namespace std;

    void main()
    {
    /*
    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 the correct result as well. * /

    setlocale(LC_ALL, “rus”);

    int num1 = 0, num2 = 0, result = 0;
    cout << "Проверка знаний таблицы умножения" << endl;
    cout <> num1;
    cout <> num2;

    cout <> result;

    if (result == num1 * num2) cout << "Вы дали верный ответ\n\n\n";
    else(result == !num1 * num2),cout << "ваш ответ не верный, correct answer:", cout << num1 * num2<<endl;

Leave a Reply

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