Lesson 4

Numbers and operations

Numbers and operations

In this lesson: you will do math: add, subtract, multiply and divide.

The whole lesson on one page

Numbers and operations

In this lesson: you will do math: add, subtract, multiply and divide.

Operations

SignMeaningExampleResult
+addition7 + 29
-subtraction7 - 25
*multiplication7 * 214
/division7 / 23.5
1тағ нарх = 12;
2тағ шумора = 3;
3чоп("Ҳамагӣ:", нарх * шумора, "сомонӣ");
4чоп("Нисф:", нарх / 2);

In SomonScript, decimal numbers are written with a dot: 3.5.

The order of operations

It works as in math: first multiplication and division, then addition and subtraction. Brackets change the order:

1чоп(2 + 3 * 4);
2чоп((2 + 3) * 4);

Area and perimeter

1тағ тӯл = 8;
2тағ бар = 5;
3тағ масоҳат = тӯл * бар;
4тағ периметр = 2 * (тӯл + бар);
5чоп("Масоҳат:", масоҳат);
6чоп("Периметр:", периметр);

Numbers and strings

5 is a number, "5" is a string. Numbers are added, strings are joined:

1чоп(5 + 5);
2чоп("5" + "5");

The result: 10 and 55. Remember this difference: you will need it in the lesson about input.

Common mistakes

  • A comma instead of a dot: 2,5. In a program, write 2.5.
  • Leaving out the multiplication sign: 2(3 + 4). Write: 2 * (3 + 4).

Task: Seconds in a day

A day has 24 hours, an hour has 60 minutes, and a minute has 60 seconds. Use arithmetic to work out the number of seconds in a day, and print it.

Input: None.

Output: One number.

Task: The yard

A yard is 12 meters long and 7 meters wide. Work out its area and perimeter and print: Масоҳат: … and Периметр: ….

Input: None.

Output: Two lines: the area and the perimeter.

Task: A purchase

A notebook costs 3 somoni, and a pen costs 2 somoni. We bought 5 notebooks and 4 pens. How many somoni did we pay in total? Print: Ҳамагӣ: ….

Input: None.

Output: Ҳамагӣ: …