Skip to main content

Ruby Programming Tutorial - Lesson 03 - Arithmetic Operations - bearshares.com


ruby.jpg
After learning basics of variables,
I am sure now you have gained enough knowledge on how to store values inside of variables.
Its time to play with variables and do some arithmetic operations on them.

Addition , adding values together.

my_age = 27
number_of_years = 3

total_age = my_age + number_of_years
print "Total Age = "
print total_age 
Capture.JPG

Subtraction , Removing value from a variable.

my_age = 27
number_of_years = 3

total_age = my_age - number_of_years
print "Total Age = "
print total_age 

It is so lovely, how easily you can become older and younger :p by just adding and removing values
in and out from your age ..

Multiplication

In this example, I am sharing with you, on how to multiply bitcoins
it is very easy actually
bitcoins = 0.2
multiplication_factor = 3

total_bitcoins = bitcoins * multiplication_factor
print "Total Bitcoins = "
print total_bitcoins 

* Operator is used to multiply two numbers together.


more arithmetic operations, which you can try
are Division,

/ this operator is used for dividing numbers

bitcoins = 0.2
multiplication_factor = 3

total_bitcoins = bitcoins / multiplication_factor
print "Total Bitcoins = "
print total_bitcoins 

I wish we could just add more friends in our lives just by using addition operator :) would be amazing.

you can also use other operators, like modulus % , which is used to find reminder after division ..

and exponential operators... you can do a little google search for more arithmetic operators.

you should be writing some programs now,
try to become as creative as you can...
think about everything you want to add in .. and subtract from ... write a code for them..

Enjoy :)


Originally posted on my blog at bearshares.com

Comments

Popular posts from this blog

Ruby Programming Tutorial - Lesson 02 - Constant values and Data types - bearshares.com

If you followed my previous lecture, you should be ready to write some ruby programs now :) Before we start .. Download and install your favorite text editor software. in my Case i am using Visual studio code. print "Hello World" Lets write our first ruby program now, creating a new file, and saving it with .rb extension will create a ruby program for us. its time to execute the code, to see if it works we can execute a ruby program doing this. ruby variable.rb Constants Now lets talk about constant values. every number like, 1, 2, 3, 1.34, 1.09, 0.0000034, or -1.98 is a constant value. it is called constant because it never changes.. like 2 is a constant value which always remains to be 2 :) Similarly each alphabet characters are also constant values like "a", "b" , "c" these are all constant values.. lets type some constant values. and see what we get when we execute our program Variables Variables are value holders

Ruby Programming Tutorial - Lesson 01 - Installation of Ruby Environment - bearshares.com

Hey Friends, This is the first part of ruby programming Lessons. In this part, we are going to learn, setting up the Ruby environment for development. I suggest doing it on linux operating system .. Ubuntu 16.04 is suggested .. If you are on windows, there is an easier method of installing ruby, that is via rubyinstaller. You can find it here :) https://rubyinstaller.org/downloads/ otherwise if you are on linux operating system, follow this guide, I am sure we are going to have so much fun. Let us Begin :) Execute this command in your terminal. we are installing ruby via rbenv .. so first lets get it git clone https://github.com/rbenv/rbenv.git ~/.rbenv After cloning rbenv execute these two commands. echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc Next step is sourcing rbenv, that you can do by typing following command source ~/.bashrc after sourcing it, you can che

Ruby Programming Tutorial - Lesson 04 -Logical Operators - bearshares.com

Logical Operators In previous article, I wrote about arithmetic operators.. in this article I will give you a list of logical operators that you will be using later on to add decision making capabilities in your programs. Yes .. that is correct your program will be able to make logical decisions and based on those decisions they do certain tasks. So lets prepare yourself, for how to write a code that makes a decision. Before we go and dig into logical operations, we need to learn this DataType which is called "Boolean" .. well you have different Datatypes in any programming language. Numbers Strings Dates and Booleans Boolean means, a value that is either True or False.. 0 or 1, you can think of it as a question whose answer is either Yes or No that's all what a boolean is. 1. OR , || Logical Operator It is used among two or more variables .. for to check whether one of the values is true .. if any single value is true among those values, result wil