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 sum = 0, a = 0, b = 0, c = 0, d = 0;

    cout <> sum;

    //Проверка на ввод корректной суммы
    if ((sum > 9999) || (sum <= 0))
    cout << "\aВведено некорректное значение" <= 1000)//первый разряд - тысячи
    {
    a = sum / 1000;
    sum -= a * 1000;
    }
    if (sum >= 100)//Второй разряд - сотни
    {
    b = sum / 100;
    sum -= b * 100;
    }
    if (sum >=10 )//Третий и четвертый разряд - десятки и еденицы
    c = sum / 10;
    d = sum - c * 10;

    //Вывод на экран числа прописью
    if (a != 0)//Вывод тысяч
    {
    if (a == 9)
    cout << "девять тысячь ";
    if (a == 8)
    cout << "восемь тысячь ";
    if (a == 7)
    cout << "семь тысячь ";
    if (a == 6)
    cout << "шесть тысячь ";
    if (a == 5)
    cout << "пять тысячь ";
    if (a == 4)
    cout << "четыре тысячи ";
    if (a == 3)
    cout << "три тысячи ";
    if (a == 2)
    cout << "две тысячи ";
    if (a == 1)
    cout << "тысяча ";
    if (((b == 0) && (c == 0)) && (d == 0))
    cout << "долларов";
    }
    if ( b != 0)//Вывод сотен 200-900
    {
    if (b == 9)
    cout << "девятьсот ";
    if (b == 8)
    cout << "восемьсот ";
    if (b == 7)
    cout << "семьсот ";
    if (b == 6)
    cout << "шестьсот ";
    if (b == 5)
    cout << "пятьсот ";
    if (b == 4)
    cout << "четыреста ";
    if (b == 3)
    cout << "триста ";
    if (b == 2)
    cout << "двести ";
    if (b == 1)
    cout << "сто ";
    }
    if (c != 0)//вывод десятков 20-90
    {
    if (c == 9)
    cout << "девяносто ";
    if (c == 8)
    cout << "восемдесят ";
    if (c == 7)
    cout << "семдесят ";
    if (c == 6)
    cout << "шестдесят ";
    if (c == 5)
    cout << "пятдесят ";
    if (c == 4)
    cout << "сорок ";
    if (c == 3)
    cout << "тридцать ";
    if (c == 2)
    cout <= 10))//вывод если 10-19
    {
    if (sum == 19)
    cout << "девятадцать ";
    if (sum == 18)
    cout << "восемнадцать ";
    if (sum == 17)
    cout << "семнадцать ";
    if (sum == 16)
    cout << "шестнадцать ";
    if (sum == 15)
    cout << "пятнадцать ";
    if (sum == 14)
    cout << "четырнадцать ";
    if (sum == 13)
    cout << "тринадцать ";
    if (sum == 12)
    cout << "двенадцать ";
    if (sum == 11)
    cout << "одинадцать ";
    if (sum == 10)
    cout < 0) && (c != 1))//вывод единиц, если они есть, и если десятки не равны единице
    {
    if (d == 9)
    cout << "девять долларов";
    if (d == 8)
    cout << "восемь долларов";
    if (d == 7)
    cout << "семь долларов";
    if (d == 6)
    cout << "шесть долларов";
    if (d == 5)
    cout << "пять долларов";
    if (d == 4)
    cout << "четыре доллара";
    if (d == 3)
    cout << "три доллара";
    if (d == 2)
    cout << "два доллара";
    if (d == 1)
    cout << "один доллар";
    }
    if ((d == 0) || (c ==1))
    cout << "долларов";
    }
    //cout << endl << a <<" " << b << " " << c << " " << d << endl;
    //cout << sum << endl;

    getch ();
    return 0;
    }

      1. I can't fix the code, who posted
        does not display:
        cout enter the amount from 0-9999
        cin – sum
        writes instead : cout – summ

      2. The tags of this site are: corner brackets. Поэтому всё от 1-й скобки перенаправления cout до 1-й скобки в cin он (форум) “съедает”.

  2. Решение третьей задачи на C#.

    using System;

    namespace proizvNum
    {
    class Program
    {
    static void Main()
    {
    int a, b, c, d;
    Console.Write(“Введите число А: “);
    a = Convert.ToInt32(Console.ReadLine());
    Console.Write(“Введите число Б: “);
    b = Convert.ToInt32(Console.ReadLine());
    c = a * b;
    Console.Write(“Как думаете, произведение А * Б = “);
    d = Convert.ToInt32(Console.ReadLine());
    if (d == c)
    {
    Console.WriteLine(“Correctly!”);
    }
    else
    {
    Console.WriteLine(“not right! Ваш ответ: ” + d + “, and the correct answer is: ” + c);
    }
    Console.WriteLine(“Press any key to exit. . .”);
    Console.ReadKey();
    }
    }
    }

    1. Why does anyone need it? (here) C#, if all the discussions here are about C++?

      P.S. C# – this is a toolkit limited applicability and only exclusively within the Windows operating system. It's a much more limited toolkit than C++.

  3. Hello, I encountered the same problem. In the console it says:”””c:\userskamo.by admindocumentsvisual studio 2013ProjectsEnterDebugEnter.exe”” is not internal or external
    team, operable program or batch file.
    Press any key to continue . . .”

    Here is the program code
    #include
    using namespace std;

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

    int Chislo(0);
    cout <> Number;

    if (Number 9999);
    cout << "Число не входит в диапазон от 1 до 9999\n";
    else if
    cout << "Вы ввели:";
    if ((Number / 1000) % 10 == 1) cout << "Одна тысяча";
    else if ((Number / 1000) % 10 == 2) cout << "Две тысячи";
    else if ((Number / 1000) % 10 == 3) cout << "Три тысячи";
    else if ((Number / 1000) % 10 == 4) cout << "Четыре тысячи";
    else if ((Number / 1000) % 10 == 5) cout << "Пять тысяч";
    else if ((Number / 1000) % 10 == 6) cout << "Шесть тысяч";
    else if ((Number / 1000) % 10 == 7) cout << "Семь тысяч";
    else if ((Number / 1000) % 10 == 8) cout << "Восемь тысяч";
    else if ((Number / 1000) % 10 == 9) cout << "Девять тысяч";

    if ((Number / 100) % 10 == 1) cout << "Сто";
    else if ((Number / 100) % 10 == 2) cout << "Двести";
    else if ((Number / 100) % 10 == 3) cout << "Триста";
    else if ((Number / 100) % 10 == 4) cout << "Четыреста";
    else if ((Number / 100) % 10 == 5) cout << "Пятьсот";
    else if ((Number / 100) % 10 == 6) cout << "Шестьсот";
    else if ((Number / 100) % 10 == 7) cout << "Семьсот";
    else if ((Number / 100) % 10 == 8) cout << "Восемьсот";
    else if ((Number / 100) % 10 == 9) cout << "Девятьсот";

    if ((Number / 10) % 10 == 1)
    {
    if (Number % 10 == 0) cout << "Десять долларов";
    else if (Number % 10 == 1) cout << "Одинадцать долларов";
    else if (Number % 10 == 2) cout << "Двенадцать долларов";
    else if (Number % 10 == 3) cout << "Тренадцать долларов";
    else if (Number % 10 == 4) cout << "Четырандцать долларов";
    else if (Number % 10 == 5) cout << "Петандцать долларов";
    else if (Number % 10 == 6) cout << "Шестнадцать долларов";
    else if (Number % 10 == 7) cout << "Семнадцать долларов";
    else if (Number % 10 == 8) cout << "Восемнадцать долларов";
    else if (Number % 10 == 9) cout << "Девятнадцать долларов";
    }

    if ((Number / 10) % 10 == 2) cout << "Двадцать";
    else if ((Number / 10) % 10 == 3) cout << "Тридцать";
    else if ((Number / 10) % 10 == 4) cout << "Сорок";
    else if ((Number / 10) % 10 == 5) cout << "Пятьдесят";
    else if ((Number / 10) % 10 == 6) cout << "Шестьсот";
    else if ((Number / 10) % 10 == 7) cout << "Семьсот";
    else if ((Number / 10) % 10 == 8) cout << "Восемьсот";
    else if ((Number / 10) % 10 == 9) cout << "Девятьсот";

    if (Number % 10 == 0)Cout << "Доларов";
    else if (Number % 10 == 1)Cout << "Один доллар";
    else if (Number % 10 == 1)Cout << "Два доллара";
    else if (Number % 10 == 1)Cout << "Три доллара";
    else if (Number % 10 == 1)Cout << "Четыре доллара";
    else if (Number % 10 == 1)Cout << "Пять долларов";
    else if (Number % 10 == 1)Cout << "Шесть долларов";
    else if (Number % 10 == 1)Cout << "Семь долларов";
    else if (Number % 10 == 1)Cout << "Восемь долларов";
    else if (Number % 10 == 1)Cout << "Девять долларов";

    return(0);
    }
    and it compiles successfully

    1. I'm sorry for that, that I bothered you in vain, made a lot of mistakes, thought , that this message in the console is not due to incorrect code writing, and until I tried to recreate the project three times, this code compiled, and only the third time he showed me my mistakes.
      Thanks for the lessons.=)

  4. Hello.
    I don’t understand how to write the code for task 3.
    Here is part of the code:
    int main()
    {
    setlocale(LC_ALL, “.1251”);

    bool product = 0;
    int answer = 0;

    cout <> production;

    —> cout <> otvet; <— This is the part of the code he reads, but it doesn't allow me to enter an answer.

    Please tell me why? Or what am I doing wrong?

  5. void main(){
    int FirstNumber, SecondNumber, Answer, Value;
    FirstNumber = SecondNumber = Answer = Value = 0;
    cout <> FirstNumber;
    cout <> SecondNumber;
    cout <> Answer;
    Value = FirstNumber * SecondNumber;
    if (Value != Answer) cout << "False " << "True: " << Value;
    else cout << "True";
    _getch();
    }

  6. solution to problem No. 3

    #include “stdafx.h”
    #include
    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
    setlocale (0,””);

    int a, b, sum;
    int y;
    do{
    cout <<endl;
    cout <> a;
    cout <<endl;
    cout <> b;
    cout <<endl;

    cout <> sum;
    cout <<endl;

    if ( sum == (a * b))
    {
    cout << " Congratulations this is the correct answer ! " ;
    }
    else
    {
    cout << " It is not right!\n";
    cout <<endl;
    cout << " The correct answer is: "<< a * b ;
    cout <<endl;

    }
    cout <<endl<<endl;
    cout << " Click to continue 1:\n" <<endl<> Y) && (Y != 0) );

    cout <<endl<<endl;
    return 0;
    }

    1. Dear David! … And all other site visitors! … Here and in all other topics!

      Don't write your solutions to problems in answers.!
      This site is very difficult to format C++ code, and in the comments it is almost impossible to format it.

      Nobody will read your unformatted C++ example code anyway.!

      1. Thank you everything is clear, I won't do it again, you have a very good site, a lot of useful things!

  7. 2I task

    #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_CTYPE, "rus");
    int a = 0;

    cout <> a;
    if (a == 1) cout << "Мизинец\n";
    else if (a == 2) cout << "Безымянный\n";
    else if (a == 3) cout << "Указательный\n";
    else if (a == 4) cout << "Указательный\n";
    else if (a == 5) cout << "Большой\n";
    else cout << "Введите номер\n";

    system("pause");
    return main();
    }

  8. 3I task

    #include "stdafx.h"
    #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");
    cout << "Программа проверки знания таблицы умножения!\n\n";

    int a,b,c;
    cout <> a;
    cout <> b;

    cout << "Какой будет результат умножения " << " " << a << "x" << b << "?\n\n";
    cout <> c;

    if (a*b == c) cout << "Ответ правельный! \n\n";
    else {
    cout << "Ответ не правельный:(\n\n";
    cout << "Правельный ответ будет - " << a*b << "\n\n";

    }

    system("pause");
    return main();
    }

Leave a Reply

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