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.

370 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. Please help. I have a really hard time with tasks.
    – – – – –
    #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. I'm not an expert, I just started learning C++ but try this.
      You didn't name the directory at the beginning and else if the loop doesn't end
      You also have an explanation in Russian, if you want it like that, add 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;
        }

  3. I spent a long time reading through the comments on the 3rd problem and I don’t understand why people don’t have an entry point for data and the result of the product.
    Here is my solution, please check and write a comment, 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. I decided to share my last task.. Everything is working,but I can't get the program to work, when entering an ambiguous number, roll back to the very beginning. Therefore the program works for any number

    #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. please comment on my logic in writing task No. 1.
    The program performs the assigned task, but it differs radically from the author’s example. so I'm worried, maybe I'm thinking in the wrong direction, It's better to fix it at the beginning of training.
    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. #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 *