Creating a Console Application
Open Visual Studio.Net from Programs in Start Menu, which opens VS.Net and displays Start Page. At the top left corner of the start page a list of recently opened projects will be displayed. If the project you want to open is available in this list, directly click on it to open it. If the project you want to open is not available in the list then click on Open Project option at the bottom of the list. For creating a new project click on Create Project option at the bottom of recent project list. This displays new project dialog box. In this dialog box select Visual CSharp language, Console Application template, provide a name to the project, specify the path where to save the project and click on ok button.
When you create a project in Csharp, it will automatically create a solution that is saved with extension .sln. Within the solution one project is created that is saved with extension .csproj. A solution is a group of projects. But by default it contains only one project and you can add any number of projects to a solution. Within a project it contains classes that are saved with extension .cs. By default project contains only one class and you can add any number of classes to a project. Current solution, projects within the solution and classes within the project are displayed as a hierarchy within solution explorer and it can be used to navigate from one project to another within the solution and from one class to another within the project. Shortcut to open solution explorer is Ctrl + Alt + L. shortcut to compile the .net application is Ctrl + Shift + B and shortcut to execute the .net application is F5 with debugging and Ctrl + F5 without debugging.
Example : The following example accepts two numbers from user and displays their sum.
namespace Language
{
class Program
{
static void Main(string[] args)
{
int A, B, R;
Console.WriteLine("Enter Two Integers");
A = int.Parse(Console.ReadLine());
B = int.Parse(Console.ReadLine());
R = A + B;
Console.WriteLine("Sum Of {0} And {1} Is {2}", A, B, R);
}
}
}
Control Statements :
Conditional Control Statements
If syntax
If(condition)
{
---
}
Else
{
---
}
Syntax2
If(Condition1)
{
---
}
Else if(Condition2)
{
---
}
Else if(Condition3)
{
---
}
Else
{
---
}
Example : The following example accepts an integer from user prints whether it is an even number or odd number.
Write a program to write find odd or even numbers in C#.NET?
namespace Language
{
class IfEvenOdd
{
static void Main()
{
int N;
Console.Write("Enter An Integer : ");
N = int.Parse(Console.ReadLine());
if (N % 2 == 0)
Console.WriteLine("Even Number");
else
Console.WriteLine("Odd Number");
}
}
}
Example : The following example accepts an integer from user and prints whether it is positive or negative.
namespace Language
{
class IfPositiveNegative
{
static void Main()
{
int N;
Console.Write("Enter An Integer : ");
N = int.Parse(Console.ReadLine());
if (N < 0)
Console.WriteLine("Negative Value");
else
Console.WriteLine("Positive Value");
}
}
}
Example : The following example accepts marks of a student in three subjects and calculates and prints total, average and grade.
Write a program to display the grades in C#.NET?
namespace Language
{
class IfMarks
{
static void Main()
{
int M1, M2, M3, Total;
float Aveg;
string Grade;
Console.WriteLine("Enter Marks In Three Subjects");
M1 = int.Parse(Console.ReadLine());
M2 = int.Parse(Console.ReadLine());
M3 = int.Parse(Console.ReadLine());
Total = M1 + M2 + M3;
Aveg = Total / 3.0F;
if (M1 < 35 || M2 < 35 || M3 < 35)
{
Grade = "Fail";
}
else
{
if (Aveg >= 90)
Grade = "Distinction";
else if (Aveg >= 70)
Grade = "First Class";
else if (Aveg >= 55)
Grade = "Second Class";
else
Grade = "Third Class";
}
Console.WriteLine("Total : {0}", Total);
Console.WriteLine("Aveg : {0}", Aveg);
Console.WriteLine("Grade : {0}", Grade);
}
}
}
Switch
Syntax
switch(expression)
{
Case result1: ---
Break;
Case result2: ---
Break;
Case result3: ---
Break;
. .
Default: ---
Break;
}
In C language writing break at the end of every case is optional. But in C# it is compulsory to write break at the end of every case event for default.
Example : The following example accepts two integers from user and performs addition or subtraction or multiplication or division when user press alphabets A or S or M or D respectively.
namespace Language
{
class Switch1
{
static void Main()
{
int A, B;
Console.WriteLine("Enter Two Integers");
A = int.Parse(Console.ReadLine());
B = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Your Choice : (A - Addition/S –
Subtraction/M - Multiplication/D - Division)");
string Ch = Console.ReadLine();
switch (Ch)
{
case "a":
case "A":
Console.WriteLine("Sum Of {0} And {1} Is {2}", A, B, A + B);
break;
case "s":
case "S":
Console.WriteLine("Difference Between {0} And {1} Is {2}",A, B, A - B);
break;
case "m":
case "M":
Console.WriteLine("Product Of {0} And {1} Is {2}", A, B,A * B);
break;
case "d":
case "D":
Console.WriteLine("Ratio Of {0} And {1} Is {2}", A, B,A / B);
break;
default:
Console.WriteLine("Invalid Choice");
break;
}
}
}
}
Looping Control Statements
While
Syntax
while(Condition)
{
---
}
Example : The following example accepts an integer and prints that integer in reverse using while loop.
namespace Language
{
class WhileReverse
{
static void Main()
{
int N, R = 0;
Console.Write("Enter An Integer : ");
N = int.Parse(Console.ReadLine());
while (N > 0)
{
R = R * 10 + N % 10;
N /= 10;
}
Console.WriteLine("Reverse Number Is {0}", R);
}
}
}
Example : The following example accepts an integer and prints its multiplication table using while loop.
Write a program to display multiplication table in C#.NET?
namespace Language
{
class WhileTable
{
static void Main()
{
int N, i=1;
Console.Write("Enter An Integer: ");
N = int.Parse(Console.ReadLine());
while (i <= 10)
{
Console.WriteLine("{0} * {1} = {2}", N, i, N * i);
i++;
}
}
}
}
Example : The following example accepts a string and prints it in reverse using while loop.
Write a program to display string in reverse order in C#.NET?
namespace Language
{
class WhileStringReverse
{
static void Main()
{
string S, R = "";
int L;
Console.Write("Enter A String : ");
S = Console.ReadLine();
L = S.Length - 1;
while (L >= 0)
{
R = R + S[L];
L--;
}
Console.WriteLine("Reverse String Is {0}", R);
}
}
}
For
Syntax
for(initialization ; condition ; inc/decofvar)
{
---
}
Example : The following example accepts an integer and prints that integer in reverse using for loop.
namespace Language
{
class ForintReverse
{
static void Main()
{
int N, R = 0;
Console.Write("Enter An Integer : ");
N = int.Parse(Console.ReadLine());
for (; N > 0; N /= 10)
{
R = R * 10 + N % 10;
}
Console.WriteLine("Reverse Number Is {0}", R);
}
}
}
Example : The following example accepts an integer and prints its multiplication table using for loop.
namespace Language
{
class ForTable
{
static void Main()
{
int N;
Console.Write("Enter An Integer : ");
N = int.Parse(Console.ReadLine());
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("{0} * {1} = {2}", N, i, N * i);
}
}
}
}
Example : The following example accepts a string and prints it in reverse using for loop.
namespace Language
{
class ForStringReverse
{
static void Main()
{
string S, R = "";
Console.Write("Enter A String : ");
S = Console.ReadLine();
for (int i = S.Length - 1; i >= 0; i--)
{
R = R + S[i];
}
Console.WriteLine("Reverse String Is {0}", R);
}
}
}
Foreach :
this is used to access one by one element from a collection like one dimensional array and is new in C#. When you don’t know the size of the array then you can use this loop to access one by one element from that array.
Syntax
foreach(datatype var in collection)
{
---
}
All the above three loops are called as entry controlled loops as they check the condition while entering in to the loop and statements in the loop will not be executed even once if the condition is false.
Do…While
Syntax
do
{
---
}while(condition);
Do…while loop is called as exit controlled loop as it checks the condition while exiting the loop. The statements in the loop will be executed at least once even when the condition is false.
Example : The following example accepts integers continuously from user until he enters zero and then displays their sum.
namespace Language
{
class DoWhileSum
{
static void Main()
{
int N, S = 0;
Console.WriteLine("Enter Integers Continuously To Calculate
Their Sum ");
Console.WriteLine("And Enter Zero To Terminate");
do
{
N = int.Parse(Console.ReadLine());
S += N;
} while (N != 0);
Console.WriteLine("Sum Is {0}", S);
}
}
}
Example : The following example accepts two integers from user and performs addition or subtraction or multiplication or division when user enters 1,2,3 or 4 as the choice respectively. This has to be continued until user enters the choice as “N”.
Write a program illustrate switch statement in C#.NET?
namespace Language
{
class DoWhileArithmetic
{
static void Main()
{
int A, B, Ch;
string Continue;
do
{
Console.WriteLine("Enter Two Integers");
A = int.Parse(Console.ReadLine());
B = int.Parse(Console.ReadLine());
Console.Write("Enter Your Choice (1 - Add/2 - Sub/3 - Mul/4 - Div) : ");
Ch = int.Parse(Console.ReadLine());
switch (Ch)
{
case 1:
Console.WriteLine("Sum Is {0}", A + B);
break;
case 2:
Console.WriteLine("Difference Is {0}", A - B);
break;
case 3:
Console.WriteLine("Product Is {0}", A * B);
break;
case 4:
Console.WriteLine("Ratio Is {0}", A / B);
break;
default:
Console.WriteLine("Wrong Choice");
break;
}
Console.Write("Do You Want To Continue? (Y/N) : ");
Continue = Console.ReadLine();
} while (Continue != "N" && Continue != "n");
}
}
}
Break And Continue
Within the looping control statements you can use the statements break and continue. Break will exit form the loop without waiting until given condition for the loop is false and continue will skip execution of statements in the loop for once and continue the loop.
Example : The following example accepts an integer and prints whether it is a prime or not.
W
namespace Language
{
class BreakPrime
{
static void Main()
{
int N,i;
Console.Write("Enter An Integer : ");
N = int.Parse(Console.ReadLine());
for (i = 2; i < N; i++)
{
if (N % i == 0)
break;
}
if (i == N)
Console.WriteLine("Prime Number");
else
Console.WriteLine("Not A Prime Number");
}
}
}
Goto
Goto is called as jumping statement and is used to jump from one location to another within the program. To jump to a location directly, a label must be defined at that location and then we have to write goto label. Using goto is not recommended as it effects the performance of the application.
Example : The following example accepts an integer and prints it in reverse using goto.
Write a program to illustrate Goto statement in C#.NET?
namespace Language
{
class GotoIntReverse
{
static void Main()
{
int N, R = 0;
Console.Write("Enter An Integer : ");
N = int.Parse(Console.ReadLine());
Reverse:
R = R * 10 + N % 10;
N /= 10;
if (N > 0)
goto Reverse;
Console.WriteLine("Reverse Number Is {0}", R);
}
}
}
For Part2 Click here
For Part4 Click here
No comments:
Post a Comment