C# Course – codemeets– 03

Today we expect Get User Input,Arrays,Methods,Return Types and finally make Simple Calculator. this all are we are doing C sharp console application. it use for get 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 program give output your name is “You entered name”.

Arrays

Array is specials type of the programming language. now we can see how to work array in C # . lets 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 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 in 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 function its return type is cube (output). main function call cube function finally get answer.

Create Calculator using C# Console

Now we try 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 out put this code doing how to covert string to float.

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

Thank for all!

More C# ClickHere!

See you Next C sharp Lesson Soon!

You may also like...

Leave a Reply

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

Translate »