![]() Here’s an exploration of the different types of calculators you can choose from. Println("There are " + groups + " groups " +Īnd that's it! Basic math in programming is both easy and fun.There may come a day in our lifetimes where kids hear the word “calculator” and think “what?!” With a shift in the way math is being taught, and new technology coming out all the time, the humble calculator may just get buried in a drawer and forgotten about.Īnd isn’t that a shame?! These helpful tools have made equations easier since the mid-1600s. Var peopleLeft = people % peoplePerGroup The % operator helps us find the remainder Var groups = Math.floor(people / peoplePerGroup) We must use Math.floor to make sure the result Var peoplePerGroup = readInt("People per group: ") * how many groups there are, and how many * total people there are, and how many people * This program helps us divide a large number ![]() The modulus (%) operator will help us find the remainder of two values.Math.floor(num1/num2) will round the result of num1 divided by num2 to an integer.This program uses a couple of key concepts: This example is a little trickier: We want to divide a large number of people into groups and show how many total people, how many people per group, how many groups, and how many people left over. Var pounds = dollars * DOLLARS_TO_POUNDS This is how we can convert from dollars Println("This program converts USD to GBP.") This program will convert dollars to pounds We can use this constant anywhere in our program, but will not want to change its value. Since we want the conversion rate to stay the same, we will create a variable at the top of our program that is in all capital letters and uses underscores between words. Then, we will multiply the conversion rate of dollars to pounds by the amount of dollars that the user specified. We will prompt the user for the amount of dollars. Let's say that we want to write a program that will convert US dollars to British pounds. There are also other shortcuts available if you want to modify the current value by using arithmetic operations. Similarly, if we want to subtract one, we can add - like: // Shortcut, subtract one This will add one to counter just like the previous code. Previously, if we wanted to add one to the current value of a variable, counter, it would look like: // Add one to the counterĪ faster way to do the same logic is to add ++ to a variable, like: // Shortcut, add one An example for this is if we want to subtract one (decrement) from a store's inventory whenever an item has been purchased. It is very common in programming to want to add one (increment) or subtract one (decrement) a variable. If we say, "What is 17 divided by 5?" The answer is 3 with 2 left over. Remember when you learned long division and you would divide two numbers and have an integer as a remainder? Modulus means divide and take the remainder. The basic rules of arithmetic apply here. There are five arithmetic operators that we can use with Javascript, most that you have likely used before. Let's see what this looks like: // Let's write a program that adds two numbers Lastly, we will add the two variables together. We will read an integer from the user and store it in our first variable, then read another integer from the user and store it in the second variable. This will allow us to add 2 numbers and print out the sum. In our first program, we are going to create a simple calculator. Now that we are able to use variables and user input, we can really start coding the fun stuff! We can do basic math! Top Down Design and Decomposition in Karel
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |