Professional Documents
Culture Documents
Dot Net LAb Manual - 5th Sem BCA
Dot Net LAb Manual - 5th Sem BCA
LAB MANUAL
1. Solve simple problem using the fundamental syntax and semantics of the C#
programming language
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary;
namespace Example1
{
class Program
{
static void Main(string[] args)
{
// WriteLine() method displays the output and provide new line character at the end of the
// string , so that the next output display in the next line.
/* Local Variables : whenever local variables are declared compulsory explicitly value should be
assigned as no default value will be assigned for local variables. */
int a=2, b=3, result=0;
result= a+b;
System.Console.WriteLine( “ Display format 1: Sum= “ + result);
System.Console.WriteLine(“ Display format 2: Sum= {0}”,result);
/* If the standard input device is the keyboard, the ReadLine method blocks control until the user
presses the Enter key. As a result used to read the input and used to pause the control. */
Console.ReadLine();
}
}
}
Output:
Hello ! Welcome to C# Programming Language.
----------------------------------------------------------
Display format 1: Sum= 5
Display format 2: Sum= 5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommandLineArg
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\nNumber of CommadLine Arguments :" + args.Length);
Console.Write("\nCommandline Arguments Are :\t");
for (int i = 0; i < args.Length; i++)
{
Console.Write(args[i] + "\t");
}
Console.ReadLine();
}
}
}
Output:
In the Command prompt
cd C:\Users\MIHIRA\Documents\Visual Studio 2010\Projects\LabManual1\CommandLineArg
csc Program.cs
Program REVA UNIVERSITY
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ValRefDemo
{
class Program
{
static int add(int i, int j)
{
int a= i; // => Local variable & formal parameter.
int b= j; //=> Local variable & formal parameter.
return (a+b);
}
Output:
IF-Else:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalculatorIF
{
class Program
{
static void Main(string[] args)
{
int num1=0,num2=0;
char ch='\0';
Console.WriteLine("Enter two integer numbers");
num1 = int.Parse(Console.ReadLine());
num2 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter a character- A/a,S/s,M/m,D/d");
ch=Console.ReadLine()[0];
// ch=Convert.ToChar(Console.ReadLine());
Output:
Switch:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalculatorSwitch
{
class Program
{
static void Main(string[] args)
{
int num1=0,num2=0;
char ch='\0';
Console.WriteLine("Enter two integer numbers");
num1 = int.Parse(Console.ReadLine());
num2 = int.Parse(Console.ReadLine());
switch (ch)
{
Console.ReadLine();
}
}
}
Output:
Conditional Operator:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PositiveNegative
{
class Program
{
static void Main(string[] args)
{
int num = 0;
string classify = "";
Console.WriteLine("Enter a number");
num = int.Parse(Console.ReadLine());
classify = (num > 0) ? "Positive" : "Negative";
Console.WriteLine("{0} number is {1}",num, classify);
Console.ReadLine();
}
}
}
Output:
For
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sum&AvgFor
{
class Program
{
static void Main(string[] args)
{
int i=0,n=0,sum=0;
double avg=0.0;
Console.WriteLine("Read 10 numbers and calculate sum and average:");
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Input the 10 numbers : \n");
for (i=1;i<=10;i++)
{
Console.Write("Number-{0} :",i);
n= Convert.ToInt32(Console.ReadLine());
sum +=n;
}
avg=sum/10.0;
Console.Write("The sum of 10 no is : {0}\nThe Average is : {1}\n",sum,avg);
}
}
}
While
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sum&AvgWhile
{
class Program
{
static void Main(string[] args)
{
int i=0,n=0,sum=0;
double avg=0.0;
Console.WriteLine("Read 10 numbers and calculate sum and average:");
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Input the 10 numbers : \n");
while(i<=10)
{
Console.Write("Number-{0} :",i);
n= Convert.ToInt32(Console.ReadLine());
sum +=n;
i++;
}
avg=sum/10.0;
Console.Write("The sum of 10 no is : {0}\nThe Average is : {1}\n",sum,avg);
}
}
Do-While
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sum&AvgDoWhile
{
class Program
{
static void Main(string[] args)
{
int i=0,n=0,sum=0;
double avg=0.0;
Console.WriteLine("Read 10 numbers and calculate sum and average:");
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Input the 10 numbers : \n");
do
{
Console.Write("Number-{0} :",i);
n= Convert.ToInt32(Console.ReadLine());
sum +=n;
i++;
} while(i<=10)
avg=sum/10.0;
Console.Write("The sum of 10 no is : {0}\nThe Average is : {1}\n",sum,avg);
}
}
}
Output:
String
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReverseStr
{
class Program
{
static void Main(string[] args)
{
String result = "";
Console.WriteLine("Enter the String");
string x = Console.ReadLine();
for (int i = x.Length - 1; i >= 0; i--)
result += x[i];
Console.WriteLine("Reversed String is :{0} ",result);
Console.ReadLine();
}
}
}
Output:
Number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReverseStr
{
class Program
{
static void Main(string[] args)
{
int n=0, reverse=0, rem=0;
Console.Write("Enter a number: ");
n = int.Parse(Console.ReadLine());
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
Console.Write("Reversed Number: "+reverse);
Console.ReadLine();
}
}
}
Output:
using System;
namespace SampleArray1
{
public class ArrayReadPrint
{
public static void Main()
{
int i;
int[] arr = new int[10];
Output:
using System;
namespace BinarySearch
{
class Program
{
public static void Main()
{
int[] a = new int[100];
Console.WriteLine("Number of elements in the array ?");
string s = Console.ReadLine();
int x = Int32.Parse(s);
Console.WriteLine("-----------------------");
Console.WriteLine(" Enter array elements ");
Console.WriteLine("-----------------------");
for (int i = 0; i < x; i++)
{
string s1 = Console.ReadLine();
a[i] = Int32.Parse(s1);
}
Console.WriteLine("--------------------");
Console.WriteLine("Enter Search element");
Console.WriteLine("--------------------");
string s3 = Console.ReadLine();
int x2 = Int32.Parse(s3);
int low = 0;
int high = x - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (x2 < a[mid])
high = mid - 1;
else if (x2 > a[mid])
low = mid + 1;
else if (x2 == a[mid])
{
Console.WriteLine("-----------------");
Console.WriteLine("Search successful");
Console.WriteLine("-----------------");
Console.WriteLine("Element {0} found at location {1}\n", x2, mid + 1);
return;
}
}
Console.WriteLine("Search unsuccessful");
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Number of elements in the array ?");
int x = Int32.Parse(Console.ReadLine());
int[] a = new int[x];
Console.WriteLine("-----------------------");
Console.WriteLine(" Enter array elements ");
Console.WriteLine("-----------------------");
for (int i = 0; i < x; i++)
{
a[i] = Int32.Parse(Console.ReadLine());
}
int t;
for (int i = 0; i <= a.Length; i++)
{
for (int j = i+1; j <= a.Length - 1; j++)
{
if (a[i] > a[j])
{
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
Console.WriteLine("The Sorted array");
for(int i=0;i<a.Length;i++) //writting array
Console.Write(a[i]+ " ");
Console.ReadLine();
}
}
}
Output:
9. Write a Program in C# to find the second largest element from Single dimensional array.
using System;
public class SecondLargest
{
public static void Main()
{
int n,i,j=0,lrg,lrg2nd;
int[] arr1 = new int[50];
for(i=0;i<n;i++)
{
if(lrg<arr1[i])
{
lrg=arr1[i];
j = i;
}
}
/* ignore the largest element and find the 2nd largest element in the array */
lrg2nd=0;
for(i=0;i<n;i++)
{
if(i==j)
{
i++; /* ignoring the largest element */
i--;
}
else
{
if(lrg2nd<arr1[i])
{
lrg2nd=arr1[i];
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BoxUnbox
{
class TestBoxingUnboxing
{
public static void Main()
{
int i = 123;
object obj = i; // IMPLICIT BOXING
Console.WriteLine("nThe value-type value = {0}", i);
Console.WriteLine("nThe object-type value = {0}", obj);
// Change the contents of i
i = 456;
Console.WriteLine("nThe value-type value = {0}", i);
Console.WriteLine("nThe object-type value = {0}", obj);
Output:
The value-type value = 123
The object –type value = 123
The value-type value = 456
The object-type value = 123
The value-type value = 123
namespace SampleClass&Object
{
class COProgram
{
int a; // non-static int field : default value is assigned automatically
static int b; // static int field: default value is assigned automatically
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SampleClass&Object
{
class A
{
int x; // non-static int field : default value is assigned automatically
static int y; // static int field: default value is assigned automatically