Ruby Course – Worldwideasy – 03

Ruby Programming lesson 3. Today we going to learn more about it. Today I will focus on Getting User Input, Hashes, Method and If Statement. Finally, Create a simple Calculator. It can provide extensive knowledge.

Getting User Input in Ruby Programming

Uses any computer language user input is how Ruby does it. But it happens here in a very different way. We are focusing on that step by step.

First, you need to install Platformio-ide-terminal on Atom text editor.

  1. Go Settings
  2. >> install
  3. Search “Platformio-ide-terminal”
  4. Install it
  5. Search “Ruby Ide” and install it

The code below explains it to you.

puts "Enter Name "
name = gets
puts ("hello" + name)

Run using CMD this code output is hello with your entered name. see below another example

puts "Enter Your Name: "
name = gets.chomp()
puts "Enter Your Age: "
age = gets.chomp()
puts ("hello" + name + "you are " +age)

Hashes in Ruby Programming

A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values ​​given by a hash function are called hash values, hash codes, digests, or simply hashes. Values ​​are used to index a fixed-size table called a hash table.

Hashes are used to shorten a word or line of words and make it very easy to code. The following example illustrates this for you.

New York >> NY
Sri Lanka >> SL
Hello World >> HW 

As mentioned above, those words are short but the output is long.

Method

Object-oriented programming is a method of action related to a message and an object. An object consists of data and behaviors; These have an interface that specifies how the object can be used by its various customers.

You have an understanding of the method Now let us see how to use this for ruby

def sayhi
  puts "Hello user"
end

puts 'top'
sayhi
puts 'bottom'

see below code output

Output

Another example

def sayhi (name)
  puts ("Hi " + name)
end

sayhi ("harry")

This Code Output is Hi harry

Create Calculator Using Ruby

We create a very good Calculator based on the knowledge we have learned about ruby ​​so far. Here you also have to learn about if

puts "Enter First Number : "
num1 = gets.chomp().to_f
puts "Enter Opartor : "
op = gets.chomp()
puts "Second Number : "
num2 = gets.chomp().to_f

if op == '+'
  puts (num1 + num2)
else if op == "-"
  puts (num1 - num2)
else if op == "*"
  puts (num1 * num2)
else if op == "/"
  puts (num1 / num2)
else
  puts "invalid Enter"
end

Here you can input two numbers as output and select an operator and perform some action

Thank you for all!

More Ruby ClickHere!

See you Next Ruby Lesson Soon!

C# Course – Worldwideasy – 03

Today we expect to Get User Input, Arrays, Methods, Return Types, and finally make a Simple Calculator. this all is we are doing C# console application. it uses forget good basic knowledge for C#.

C# User Input

Here we take user input and inquire how to perform a certain function. It can make more creative creations. You can get an understanding of it in the following way. See Below Example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Your Name : ");
            String name = Console.ReadLine();
            Console.WriteLine("Your Name is " + name);
            Console.ReadLine();
        }
    }
}

This Code output is Program ask your name you can type it. after the program give output your name is “You entered the name”.

Arrays

The array is a specials type of programming language. now we can see how to work array in C # . let’s see below code example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = { 1, 2, 3, 4, 5, 6 };
            Console.WriteLine(num[3]);
            Console.ReadLine();
        }
    }
}

This code output is 4. Array using the same data type list add to the program.

Method Using C#

Here we take user input and inquire how to perform a certain function. It can make more creative creations. You can get an understanding of it in the following way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            sayhi();
            Console.WriteLine("How you");
            sayhi();
            Console.ReadLine();
        }
        static void sayhi()
        {
            Console.WriteLine("Hello World");
        }
    }
}

This code output is Hello World How You Hello World. You can try this code on your Computer.

Return Type

What is done here is to show the output that we need to take into another function in a process that is done in a method. The following example code will make it clear to you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(cube(5));
            Console.ReadLine();
        }
        static int cube (int num)
        {
            int cube = num * num * num;
            return cube;
        }
       }
}

This code output is 125. you can see cube is a function its return type is a cube (output). main function call cube function finally get an answer.

Create Calculator using C# Console

Now we try to Create Calculator using method, input, and others. see below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Number one : ");
            String num01 = Console.ReadLine();
            Console.WriteLine("Enter Number two : ");
            String num02 = Console.ReadLine();
            float num1 = float.Parse(num01);
            float num2 = float.Parse(num02); //convert string to float
            Console.WriteLine("Answers is: ");
            Console.WriteLine("Add = "+add(num1,num2));
            Console.WriteLine("Cut = "+cut(num1, num2));
            Console.WriteLine("Multification = "+mul(num1, num2));
            Console.WriteLine("Sub = "+sub(num1, num2));
            Console.ReadLine();

        }
        static float add(float num1,float num2)
        {
            float total = num1 + num2;
            return total;
        }//add

        static float cut(float num1,float num2)
        {
            float cut = num1 - num2;
            return cut;
        }//cut
        static float mul(float num1, float num2)
        {
            float mul = num1 * num2;
            return mul;
        }//mul
        static float sub(float num1, float num2)
        {
            float sub = num1 / num2;
            return sub;
        }//sub

       }
}

You can see below output this code doing how to convert string to float.

Now you have good knowledge of this programming language of today’s lesson. practice this code and develop your knowledge.

Thank you for all!

More C# ClickHere!

See you Next C# Lesson Soon!