The basics of programming in c++ for beginners

Tasks: Data types, variables and constants

Here are collected the task to the article Data types, variables and constants. Try to solve problems on their own and only need to look at the decision. So you will learn much faster programming fundamentals.

1. Declare variables with which you can calculate the total amount of the purchase of several products. For example a chocolate bar, coffee and packets of milk.

2. Declare three variables of type int, and the first to assign a numeric value, the second variable is the first variable increased by 3, and the third variable is equal to the sum of the first two.

3. Declare variables, to count the total number of objects for the table. for example cups, the same number of saucers and spoons.

122 thoughts on “Tasks: Data types, variables and constants

  1. And this is the first task:
    #include
    #include

    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Russian”);
    int a, b, c, sum;
    int k, l, j;
    cout <> k >> a;
    cout <> l >> b;
    cout <> j >> c;

    sum =k* a + l * b + j * c;
    cout << "Сумма : " << sum << endl;

    system("pause");
    return 0;

    }

  2. Second, even so it is best))

    [code]#include

    using namespace std;
    void main()
    {
    int od ;
    cout <> from;
    two of int + 3 =;
    int = three + two of;

    cout << "pervoe= : ";
    cout << from << "\n";
    cout << "vtoroe= : ";
    cout << dva << "\n";
    cout << "tretie= : ";
    cout << tri << "\n";
    system ("pause") ;
    }[/code]

    1. #include

      using namespace std;
      void main()
      {
      int od ;
      cout <> from;
      two of int + 3 =;
      int = three + two of;

      cout << "pervoe= : ";
      cout << from << "\n";
      cout << "vtoroe= : ";
      cout << dva << "\n";
      cout << "tretie= : ";
      cout << tri << "\n";
      system ("pause") ;
      }

  3. so did not go?(1 a task)

    #include

    int main()
    {
    int shokolad=4;
    int kofe=3;
    int moloko = 5;

    float cenaShokolad=10.4;
    float cenaKofe=6.3;
    float cenaMoloko=3.7;
    float summ=(shokolad * cenaShokolad)+(kofe * cenaKofe)+(Moloko * cenaMoloko);
    printf(“Obshay summa pokupki sostavlyet% .2f $ n”,summ);
    return(0);
    }

    1. It almost does not differ from that, which leads the author … but to use printf() to output the result instead of outputting a stream cout.

      But! printf() – is a C style, rather than C ++, and C ++ code it better, no reasonable need for it, do not use, and in use the output stream cout.

      1. It is not that simple – sometimes it works, sometimes not (depending on the type of operating system, Localization of the system and others.). Questions localization quite confused, and in order, to clarify them, very soon it will be posted here on the site 3 small articles on localization and input-output lines of the Russian-speaking.

        A first approximation, You can write well:
        setlocale( LC_ALL, “Russian” );
        Because your "rus", will work, for example, в Windows, but will fail on Linux. A site on the C ++ at all, instead of using C ++ in Windows.

    2. Personally, I would write like this:

      #include

      using namespace std;

      int main()
      {
      int shokolad=4;
      int kofe=3;
      int moloko = 5;

      float cenaShokolad=10.4;
      float cenaKofe=6.3;
      float cenaMoloko=3.7;
      float summ = (shokolad * cenaShokolad)+(kofe * cenaKofe)+(Moloko * cenaMoloko);
      cout << "Obshay summa pokupki sostavlyet – " << summ << endl;

      return(0);
      }

  4. Is vtoroya job so not easy?

    #include “stdafx.h”
    #include

    using namespace std;

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

    int a,b,c;
    a = 2;
    b = a + 3;
    c = a + b;
    cout << "c= " << c << endl;

    return 0;
    }

    1. The simpler?
      You could write down, that “easier” (and it is right and will work):

      int a, b, c;
      b = ( a = 2 ) + 3;
      cout << "c = " << ( c = a + b ) << endl;

      “easier” – it does not mean better!

  5. #include
    using namespace std;

    int main () {
    setlocale (0, “”);
    int head, delusion, Log;

    cout <> cap;

    int amount = cap * 3;
    cout << "Всего потребуется предметов " << amount << endl;
    return 0;
    }

    1. Another implementation of the third objective.. after imaging the first embodiment proved erroneously

      #include
      using namespace std;

      int main () {
      setlocale (0, “”);
      int head, delusion, Log;

      cout <> cap;

      int amount = cap * 3;
      cout << "Всего потребуется предметов " << amount << endl;
      return 0;
      }

  6. And this is how you can solve the first problem?

    #include
    using namespace std;

    int main() {
    const float chp = 55.0;
    const float cp = 125.5;
    const float mp = 29.99;

    int ch, c, m;
    cin>>ch;
    cin>>c;
    cin>>m;
    float sum = ch*chp + c*cp + m*mp;
    cout<<"chocolate price = "<<chp*ch<<endl;
    cout<<"coffee price = "<<cp*c<<endl;
    cout<<"milk price = "<<mp*m<<endl;
    cout<<"sum = "<<sum<<endl;
    return 0;
    }

  7. most likely with English everything is shit, I'm too lazy to set the locale
    how do you like the code?)

    #include “stdafx.h”
    #include

    using namespace std;
    int main()
    {
    int a, b, c;
    float j, k, l;
    cout <> a;
    cout <> b;
    cout <> c;
    cout <> j;
    cout <> k;
    cout <> l;
    float sum = a * j + b * k + c * l;
    cout << "amount of your purchase = " << sum << endl;
    system("pause");
    return 0;

    }

Leave a Reply

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