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.

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

  1. #include “stdafx.h”
    #include

    int main()
    {
    std::cout <> a >> b;

    std::cout << "How much will " << a << "*" << b << "?" << std::endl;

    std::cout <> c;

    if (a * b == c) std::cout << "Well done, the answer is correct!\n";
    else if (a * b != c) std::cout << "Sorry, the answer is not true! Go study! :(\n";

    return 0;
    }

  2. Прошу помочь. У меня совсем туго даются задачи.
    – – – – –
    #include
    using namespace std;

    int main()
    {
    int a = 0;
    int b = 0;
    int c = a * b;

    cout <> a;
    cin >> b;

    cout << "Результат умножения a * b ?";

    if (c == a*b)
    cout << "Правильно" << endl;
    else if (c!== a * b)
    cout << "Неправльно! Answer: " << c == a * b << endl << end;

    system("pause");
    return 0;
    }

    1. Я не эксперт, только начал изучать С++ но попытайся так.
      Ты не назвал директорию в начале и else if цикл не заканчивается
      Еще у тебя пояснение на русском, если хочешь так то добавляй setlocale(LC_ALL, “rus”);

      #include
      using namespace std;

      int main()
      {
      int a,b,c;

      cout <> a;
      cout <> b;
      cout <> c;

      if (c == a * b)
      cout << "verno"<<endl;
      else
      cout << "ne verno "<<endl;

      return 0;
      }

      1. #include
        using namespace std;

        int main()
        {
        int a,b,c;

        cout <> a;
        cout <> b;
        cout <> c;

        if (c == a * b)
        cout << "verno"<<endl;
        else
        cout << "ne verno "<<endl;

        return 0;
        }

      2. Почему нормально скопировать скрипт не получается. cin, \n проглатило? Хрень какая.

  3. долго вчитывался в комментарии по 3ей задачи и не понимаю почему у людей нет пункта ввода данных и результата произведения.
    Here is my solution, прошу проверить и написать комментарий, Thank you.
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;

    cout <> a;

    cout <> b;

    cout <> d;

    c = a * b;

    if (d == c)
    cout << "Otvet veren.";

    if (d != c)
    cout << "Otvet ne veren, " << "Pravil'nii otvet = " << c << endl;

  4. Решила поделиться последним заданием. Everything is working,но не могу заставить программу, при вводе неоднозначного числа откатываться в самое начало. Поэтому программа работает на любое число

    #include
    using namespace std;

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

    int FirstNumber = 0;
    int SecondNumber = 0;
    int answer;
    cout<<"Программа для проверки знаний по таблице умножения."<<endl;
    cout<<"=================================================="<<endl;
    cout<<"Введите два однозначных числа"<<endl;
    cout<>FirstNumber;
    cout<>SecondNumber;
    cout<<"Ответ:"<<FirstNumber<<"*"<<SecondNumber<>answer;

    if (FirstNumber*SecondNumber==answer) cout<<"Ответ правильный!";
    else if (FirstNumber*SecondNumberanswer) {cout<<"Ответ не правильный!"<<endl;
    answer=FirstNumber*SecondNumber;
    cout<<"Правильный ответ:"<<endl<<FirstNumber<<"*"<<SecondNumber<<"="<<answer;
    }
    return 0;
    }

  5. прошу прокомментировать мою логику в написании задачи №1.
    поставленную задачу программа выполняет, но вот отличается от примера автора кардинально. посему я беспокоюсь вдруг я в неправильном направлении думаю, лучше исправить вначале обучения.
    Thank you.

    #include
    using namespace std;

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

    int a(0);
    cout <> a;
    cout <= 1 && a <= 9999)
    {
    if ((a / 1000) % 10)
    {
    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 cout << "девять тысяч ";
    a %= 1000;
    }

    if ((a / 100) % 10)
    {
    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 cout << "девятьсот ";
    a %= 100;
    }

    if ((a / 10) % 10)
    {
    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 << "девяносто ";

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

    cout <= 2 && a % 10 <= 4) cout <= 5 && a % 10 <= 9 || a == 0) cout << "ов";
    cout << " США\n";
    return 0;
    }
    a %= 10;
    }

    if (a)
    {
    if (a == 1) cout << "один ";
    else if (a == 2) cout << "два ";
    else if (a == 3) cout << "три ";
    else if (a == 4) cout << "четыре ";
    else if (a == 5) cout << "пять ";
    else if (a == 6) cout << "шесть ";
    else if (a == 7) cout << "семь ";
    else if (a == 8) cout << "восемь ";
    else cout << "девять ";
    }
    cout <= 2 && a % 10 <= 4) cout <= 5 && a % 10 <= 9 || a == 0) cout << "ов";
    cout << " США\n";
    }
    else cout << "неверная сумма, повторите ввод \n";

    return 0;
    }

  6. вообще каша получилась. как скинуть текст в комменте без потери данных?

  7. #include
    #include

    using namespace std;

    /* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

    int main(int argc, char** argv) {

    int firstValue;
    int secondValue;

    cout <> firstValue;

    if (firstValue > 9)
    {
    cout << "\nyou must enter value from 1 to 9\n";
    }
    else if (firstValue < 1)
    {
    cout << "\nyou must enter value from 1 to 9\n";
    }

    cout <> secondValue;
    if (secondValue > 9)
    {
    cout << "\nyou must enter value from 1 to 9\n";
    }
    else if (secondValue < 1)
    {
    cout <<"\nyou must enter value from 1 to 9\n";
    }

    int userAnswer;
    int rightAnswer;
    rightAnswer = firstValue * secondValue;

    cout << firstValue << " * " << secondValue <> userAnswer;
    if (userAnswer==rightAnswer)
    {
    cout << "\nAnswer is right!\n";
    }
    else
    {
    cout << "\nYou lost! Right answer is: " << rightAnswer << "\n";
    }

    return 0;
    }

Leave a Reply

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