C# Course – codemeets– 03
Today, we will cover Getting User Input, Arrays, Methods, and Return Types, and finally, we will create a Simple Calculator using a C# console application. This will help build a strong foundation in C# programming.
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
An array is a special type in programming that allows storing multiple values in a single variable. Now, let’s see how arrays work in C# with the example code below.
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 determine how to execute a specific function. This allows for more dynamic and interactive programs. The example below will help you understand this concept better
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
Here, we pass the required output to another function using a method. This helps in structuring the code efficiently. The example below will clarify this concept.
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!
See you Next C sharp Lesson Soon!