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. Nih * shenki is not clear in the 1st task.
    how, bl * db, 10 % 8 = 8, if % – this is the remainder of the division?!?!??!?!?!?!??!?!?!?!??!?!?
    And it should turn out 2, but not 8.

    1. Hi. There is modulo 10 everywhere. %10. and 8%10 there will be eight. there is even an online calculator. see here we enter for example 700 he crawls through the code until he finds a suitable condition else if ((enterNumber / 100) % 10 == 7) cout << "семьсот "; 700divide by one hundred is seven and seven divide modulo by 10 this seven condition is fulfilled

  2. #include
    using namespace std;
    int main()

    {
    setlocale(LC_ALL, “Russian”);

    cout << "Введите целое число " <> from;
    cout << "Введите второе целое число " <> a;
    int f = z * a;
    cout << " How many will be " << from << " multiplied by " << a << "?" <> s;
    if (f == s)

    cout << " Результат верен ";

    else if (f != s)

    cout << " The result is not correct! " << from << " multiplied by " << a << " equal to " << f << endl;
    }

    1. This is a logical error because. the wrong result is checked again for incorrectness. Why again if if the result is not correct, then it is enough to leave the output of the correct answer after the else.

  3. #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Russian”);
    cout <> from;
    if (with == 1)
    cout << " Это большой\n";
    if (with == 2)
    cout << " Это указательный\n";
    if(z == 3)
    cout << " Это средний\n";
    if (with == 4)
    cout << " Это безымянный\n";
    if (with == 5)
    cout < 5 || from < 1)
    cout << " Вы выбрали число не из предложенных\n";
    }

  4. #include
    #include
    using namespace std;
    int main() {
    setlocale(LC_ALL, “rus”);
    int a;
    string q;

    cout << "enter number 1-9999 " <> a;
    cout << "выдерите валюту (dollars(D), rubles(R), pounds(F)): " <> q;
    if (q == “D” || q == “R” || q == “F”)
    {

    if ((a / 1000) % 10 == 1) cout << "одна тясяча ";
    else if ((a / 1000) % 10 == 2) cout << "две тясячи ";
    else if ((a / 1000) % 10 == 3) cout << "три тясячи ";
    else if ((a / 1000) % 10 == 4) cout << "четыре тясячи ";
    else if ((a / 1000) % 10 == 5) cout << "пять тясячь ";
    else if ((a / 1000) % 10 == 6) cout << "шесть тясячь ";
    else if ((a / 1000) % 10 == 7) cout << "семь тясячь ";
    else if ((a / 1000) % 10 == 8) cout << "восемь тясячь ";
    else if ((a / 1000) % 10 == 9) cout << "девять тясячь ";

    if ((a / 100) % 10 == 1) cout << "сто ";
    else if ((a / 100) % 10 == 2) cout << "двести ";
    else if ((a / 100) % 10 == 3) cout << "триста ";
    else if ((a / 100) % 10 == 4) cout << "четыреста ";
    else if ((a / 100) % 10 == 5) cout << "пятьсот ";
    else if ((a / 100) % 10 == 6) cout << "шестьсот ";
    else if ((a / 100) % 10 == 7) cout << "семьсот ";
    else if ((a / 100) % 10 == 8) cout << "восемьсот ";
    else if ((a / 100) % 10 == 9) cout << "девятьсот ";

    if ((a / 10) % 10 == 2) cout << "двадцать ";
    else if ((a / 10) % 10 == 3) cout << "тридцать ";
    else if ((a / 10) % 10 == 4) cout << "сорок ";
    else if ((a / 10) % 10 == 5) cout << "пятьдесят ";
    else if ((a / 10) % 10 == 6) cout << "шестьдесят ";
    else if ((a / 10) % 10 == 7) cout << "семьдесят ";
    else if ((a / 10) % 10 == 8) cout << "восемдесят ";
    else if ((a / 10) % 10 == 9) cout <= 10 && a % 100 <= 19)
    {
    if (a % 100 == 11) cout << "одинадцать долларов ";
    else if (a % 100 == 12) cout << "двянадцать долларов ";
    else if (a % 100 == 13) cout << "тринадцать долларов ";
    else if (a % 100 == 14) cout << "четырнадцать долларов ";
    else if (a % 100 == 15) cout << "пятнадцать долларов ";
    else if (a % 100 == 16) cout << "шестнадцать долларов ";
    else if (a % 100 == 17) cout << "семнадцать долларов ";
    else if (a % 100 == 18) cout << "восемнадцать долларов ";
    else if (a % 100 == 19) cout << "девятнадцать долларов ";
    else if (a % 100 == 10) cout << "десять долларов ";
    }
    else
    {
    if (a % 10 == 1) cout << "один доллар ";
    else if (a % 10 == 2) cout << "два доллара ";
    else if (a % 10 == 3) cout << "три доллара ";
    else if (a % 10 == 4) cout << "четыре доллара ";
    else if (a % 10 == 5) cout << "пять долларов ";
    else if (a % 10 == 6) cout << "шесть долларов ";
    else if (a % 10 == 7) cout << "семь долларов ";
    else if (a % 10 == 8) cout << "восемь долларов ";
    else if (a % 10 == 9) cout << "девять долларов ";
    else cout <= 10 && a % 100 <= 19)
    {
    if (a % 100 == 11) cout << "одинадцать рублей ";
    else if (a % 100 == 12) cout << "двянадцать рублей ";
    else if (a % 100 == 13) cout << "тринадцать рублей ";
    else if (a % 100 == 14) cout << "четырнадцать рублей ";
    else if (a % 100 == 15) cout << "пятнадцать рублей ";
    else if (a % 100 == 16) cout << "шестнадцать рублей ";
    else if (a % 100 == 17) cout << "семнадцать рублей ";
    else if (a % 100 == 18) cout << "восемнадцать рублей ";
    else if (a % 100 == 19) cout << "девятнадцать рублей ";
    else if (a % 100 == 10) cout << "десять рублей ";
    }
    else
    {
    if (a % 10 == 1) cout << "один рубль ";
    else if (a % 10 == 2) cout << "два рубля ";
    else if (a % 10 == 3) cout << "три рубля ";
    else if (a % 10 == 4) cout << "четыре рубля ";
    else if (a % 10 == 5) cout << "пять рублей ";
    else if (a % 10 == 6) cout << "шесть рублей ";
    else if (a % 10 == 7) cout << "семь рублей ";
    else if (a % 10 == 8) cout << "восемь рублей ";
    else if (a % 10 == 9) cout << "девять рублей ";
    else cout <= 10 && a % 100 <= 19)
    {
    if (a % 100 == 11) cout << "одинадцать фунтов ";
    else if (a % 100 == 12) cout << "двянадцать фунтов ";
    else if (a % 100 == 13) cout << "тринадцать фунтов ";
    else if (a % 100 == 14) cout << "четырнадцать фунтов ";
    else if (a % 100 == 15) cout << "пятнадцать фунтов ";
    else if (a % 100 == 16) cout << "шестнадцать фунтов ";
    else if (a % 100 == 17) cout << "семнадцать фунтов ";
    else if (a % 100 == 18) cout << "восемнадцать фунтов ";
    else if (a % 100 == 19) cout << "девятнадцать фунтов ";
    else if (a % 100 == 10) cout << "десять фунтов ";
    }
    else
    {
    if (a % 10 == 1) cout << "один фунт ";
    else if (a % 10 == 2) cout << "два фунта ";
    else if (a % 10 == 3) cout << "три фунта ";
    else if (a % 10 == 4) cout << "четыре фунта ";
    else if (a % 10 == 5) cout << "пять фунтов ";
    else if (a % 10 == 6) cout << "шесть фунтов ";
    else if (a % 10 == 7) cout << "семь фунтов ";
    else if (a % 10 == 8) cout << "восемь фунтов ";
    else if (a % 10 == 9) cout << "девять фунтов ";
    else cout << "фунтов";
    }
    }
    }
    else cout << "валюта выбрана не правильно";

    return 0;
    }

  5. #include
    using namespace std;

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

    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    cout << "проверьте себя на знание таблицы умножения" << endl;
    cout << "введите первоее число" <> a;
    if (a 10) {
    cout << "вы ввели число вне диапазона" << endl;
    }
    else

    cout << "введите второе число" <> b;
    if (b 10)

    cout << "вы ввели число вне диапазона" << endl;
    else

    cout << "введите ваш ответ " <> d;
    c = a * b;
    if (d == c)
    cout << "правильно! answer " << d << endl;
    else
    cout << "не верно! the multiplication result " << a << " on " << b << " – " << c << endl;

    return 0;
    }

    please tell me how to write the code so that after writing a number out of range or let's say after the program ends, you can start over?

    1. #include
      #include
      using namespace std;

      int main(int argc, char *argv[])
      {
      int a, b, c, d;

      cout << "Умножение проверь себя" <> a;
      cin >> b;
      c = a*b;
      cout <> d;
      if(d == c){
      cout << c << "Правильно!";

      }else{
      cout << "неправильно";
      }
      }

    2. Try the goto statement,or an array to start over.
      Check the range and completion of the program using if else or switch case

  6. setlocale(LC_ALL, “rus”);
    int number1, number2, number3,sum;
    cout << "Введите число 1" <> number1;
    cout << "Введите число 2" <> number2;
    sum = (number1 * number2);

    cout << "Введите произведение двух чисел:" <> number3;

    if ((number1 * number2) == number3)
    cout << "Результат верен" << endl;
    else
    cout << "Результат не верен"<< " " << "правильный ответ = " << sum << endl;

  7. #include
    #include
    using namespace std;

    int main(int argc, char *argv[])
    {
    int a, b, c, d;

    cout << "Умножение проверь себя" <> a;
    cin >> b;
    c = a*b;
    cout <> d;
    if(d == c){
    cout << c << "Правильно!";

    }else{
    cout << "неправильно";
    }
    }

  8. #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    char a;
    cout <> a;
    switch (a)
    {
    case ‘1’:
    cout << "Большой" << endl;
    break;
    case '2':
    cout << "Указательный" << endl;
    break;
    case '3':
    cout << "Средний" << endl;
    break;
    case '4':
    cout << "безымянный" << endl;
    break;
    case '5':
    cout << "Мизинец" << endl;
    break;

    default:
    cout << "Ощибка" << endl;
    }

    system("pause");
    return 0;
    }

Leave a Reply

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