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.

Output

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!

Leave a Comment

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

Translate »