Course Code Duration Course Title L T P C
BPAL15F5810 16 Weeks .Net programming Lab 0 0 2 2
LAB MANUAL
1. Solve simple problem using the fundamental syntax and semantics of the C#
programming language
Create a Console Application Project:
using System;
using [Link];
using [Link];
using [Link];
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.
[Link]("Hello ! Welcome to C# Programming Language”);
[Link]( “----------------------------------------------------------“);
/* 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;
[Link]( “ Display format 1: Sum= “ + result);
[Link](“ 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. */
[Link]();
}
}
}
Output:
Hello ! Welcome to C# Programming Language.
----------------------------------------------------------
Display format 1: Sum= 5
Display format 2: Sum= 5
2. Write a Program in C# to demonstrate Command line arguments processing.
using System;
using [Link];
using [Link];
using [Link];
namespace CommandLineArg
{
class Program
{
static void Main(string[] args)
{
[Link]("\nNumber of CommadLine Arguments :" + [Link]);
[Link]("\nCommandline Arguments Are :\t");
for (int i = 0; i < [Link]; i++)
{
[Link](args[i] + "\t");
}
[Link]();
}
}
}
Output:
In the Command prompt
cd C:\Users\MIHIRA\Documents\Visual Studio 2010\Projects\LabManual1\CommandLineArg
csc [Link]
Program REVA UNIVERSITY
Number of CommadLine Arguments: 2
CommandLine Arguments are: REVA UNIVERSITY
3. Write a program in c# to implement stack memory operations
using System;
using [Link];
using [Link];
using [Link];
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);
}
static void Main(string[] args)
{
/* All value types whether declared inside function or declared as method parameter then always
stored on to the satck. Even the address of the function call is stored in stack */
int m= 4; // => Local variable
int n = 5; //=> Local variable
int result = add( m,n); //=> Fuction call : parameter associated is actual parameters
[Link]( “The sum of two number is: {0}”,result);
[Link]();
}
}
}
Output:
The sum of two number is: 9
4. Write C# programs that use selection (if, switch, conditional operator)
IF-Else:
using System;
using [Link];
using [Link];
using [Link];
namespace CalculatorIF
{
class Program
{
static void Main(string[] args)
{
int num1=0,num2=0;
char ch='\0';
[Link]("Enter two integer numbers");
num1 = [Link]([Link]());
num2 = [Link]([Link]());
[Link]("Enter a character- A/a,S/s,M/m,D/d");
ch=[Link]()[0];
// ch=[Link]([Link]());
if(ch =='A'|| ch =='a'){
value = num1 + num2;
[Link]("Addition of {0} and {1} is {2}", num1, num2, value);
}
else if(ch =='S' || ch=='s'){
value = num1-num2;
[Link]("Subtraction of {0} and {1} is {2}", num1, num2, value);
}
else if(ch=='M'|| ch =='m'){
value = num1 * num2;
[Link]("Multiplication of {0} and {1} is {2}", num1, num2, value);
}
else if (ch == 'D' || ch == 'd'){
value=num1/num2;
[Link]("Division of {0} and {1} is {2}", num1, num2, value);
}
else{
[Link]("Please enter either A/a for addition, S/s for subtraction, M/m for
multiplication and D/d for division");
}
[Link]();
}
}
}
Output:
Switch:
using System;
using [Link];
using [Link];
using [Link];
namespace CalculatorSwitch
{
class Program
{
static void Main(string[] args)
{
int num1=0,num2=0;
char ch='\0';
[Link]("Enter two integer numbers");
num1 = [Link]([Link]());
num2 = [Link]([Link]());
[Link]("Enter an Arithmetic Operator (+,-,*,/) ");
ch = [Link]()[0];
// ch=[Link]([Link]());
switch (ch)
{
case '+': value = num1 + num2;
[Link]("Addition of {0} and {1} is {2}", num1, num2, value);
[Link]();
break;
case '-': value = num1 - num2;
[Link]("Subtraction of {0} and {1} is {2}", num1, num2, value);
[Link]();
break;
case '*':
value = num1 * num2;
[Link]("Multiplication of {0} and {1} is {2}", num1, num2, value);
[Link]();
break;
case '/': value = num1 / num2;
[Link]("Division of {0} and {1} is {2}", num1, num2, value);
[Link]();
break;
default: [Link]("Please enter either + for addition, - for subtraction, *
for multiplication and / for division");
[Link]();
break;
}
[Link]();
}
}
}
Output:
Conditional Operator:
using System;
using [Link];
using [Link];
using [Link];
namespace PositiveNegative
{
class Program
{
static void Main(string[] args)
{
int num = 0;
string classify = "";
[Link]("Enter a number");
num = [Link]([Link]());
classify = (num > 0) ? "Positive" : "Negative";
[Link]("{0} number is {1}",num, classify);
[Link]();
}
}
}
Output:
5. Write C# programs that use loops (while, do while, for)
For
using System;
using [Link];
using [Link];
using [Link];
namespace Sum&AvgFor
{
class Program
{
static void Main(string[] args)
{
int i=0,n=0,sum=0;
double avg=0.0;
[Link]("Read 10 numbers and calculate sum and average:");
[Link]("----------------------------------------------");
[Link]("Input the 10 numbers : \n");
for (i=1;i<=10;i++)
{
[Link]("Number-{0} :",i);
n= Convert.ToInt32([Link]());
sum +=n;
}
avg=sum/10.0;
[Link]("The sum of 10 no is : {0}\nThe Average is : {1}\n",sum,avg);
}
}
}
While
using System;
using [Link];
using [Link];
using [Link];
namespace Sum&AvgWhile
{
class Program
{
static void Main(string[] args)
{
int i=0,n=0,sum=0;
double avg=0.0;
[Link]("Read 10 numbers and calculate sum and average:");
[Link]("----------------------------------------------");
[Link]("Input the 10 numbers : \n");
while(i<=10)
{
[Link]("Number-{0} :",i);
n= Convert.ToInt32([Link]());
sum +=n;
i++;
}
avg=sum/10.0;
[Link]("The sum of 10 no is : {0}\nThe Average is : {1}\n",sum,avg);
}
}
Do-While
using System;
using [Link];
using [Link];
using [Link];
namespace Sum&AvgDoWhile
{
class Program
{
static void Main(string[] args)
{
int i=0,n=0,sum=0;
double avg=0.0;
[Link]("Read 10 numbers and calculate sum and average:");
[Link]("----------------------------------------------");
[Link]("Input the 10 numbers : \n");
do
{
[Link]("Number-{0} :",i);
n= Convert.ToInt32([Link]());
sum +=n;
i++;
} while(i<=10)
avg=sum/10.0;
[Link]("The sum of 10 no is : {0}\nThe Average is : {1}\n",sum,avg);
}
}
}
Output:
Read 10 numbers and calculate sum and average:
-----------------------------------------------------------------
Number-1: 10
Number-2: 20
Number-3: 30
Number-4: 40
Number-5: 50
Number-6: 60
Number-7: 70
Number-8: 80
Number-9: 90
Number-10: 100
The sum of 10 natural number is : 450
The avg is: 55
6. Write a program to reverse a given string and number using C#
String
using System;
using [Link];
using [Link];
using [Link];
namespace ReverseStr
{
class Program
{
static void Main(string[] args)
{
String result = "";
[Link]("Enter the String");
string x = [Link]();
for (int i = [Link] - 1; i >= 0; i--)
result += x[i];
[Link]("Reversed String is :{0} ",result);
[Link]();
}
}
}
Output:
Number
using System;
using [Link];
using [Link];
using [Link];
namespace ReverseStr
{
class Program
{
static void Main(string[] args)
{
int n=0, reverse=0, rem=0;
[Link]("Enter a number: ");
n = [Link]([Link]());
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
[Link]("Reversed Number: "+reverse);
[Link]();
}
}
}
Output:
Enter the number: 345
Reversed number: 543
7. Write C# programs that reads and prints One-dimensional arrays.
using System;
namespace SampleArray1
{
public class ArrayReadPrint
{
public static void Main()
{
int i;
int[] arr = new int[10];
[Link]("\n\nRead and Print elements of an array:\n");
[Link]("-----------------------------------------\n");
// Read the Array elements
[Link]("Input 10 elements in the array :\n");
for(i=0; i<10; i++)
{
[Link]("element - {0} : ", i);
arr[i] = Convert.ToInt32([Link]());
}
// Display the Array elements
[Link]("\nElements in array are: ");
for(i=0; i<10; i++)
{
[Link]("{0} ", arr[i]);
}
[Link]("\n");
}
}
}
Output:
Read and Prints elements of an array
---------------------------------------------
Input 10 elements in the array
10 20 30 40 50 60 70 80 90 100
Elements in array are:
10 20 30 40 50 60 70 80 90 100
8. Write a C# program to search an element using Binary Search.
using System;
namespace BinarySearch
{
class Program
{
public static void Main()
{
int[] a = new int[100];
[Link]("Number of elements in the array ?");
string s = [Link]();
int x = [Link](s);
[Link]("-----------------------");
[Link](" Enter array elements ");
[Link]("-----------------------");
for (int i = 0; i < x; i++)
{
string s1 = [Link]();
a[i] = [Link](s1);
}
[Link]("--------------------");
[Link]("Enter Search element");
[Link]("--------------------");
string s3 = [Link]();
int x2 = [Link](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])
{
[Link]("-----------------");
[Link]("Search successful");
[Link]("-----------------");
[Link]("Element {0} found at location {1}\n", x2, mid + 1);
return;
}
}
[Link]("Search unsuccessful");
}
}
}
Output:
8. Write a C# program to sort array of elements using Bubble sort.
using System;
using [Link];
using [Link];
using [Link];
namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
[Link]("Number of elements in the array ?");
int x = [Link]([Link]());
int[] a = new int[x];
[Link]("-----------------------");
[Link](" Enter array elements ");
[Link]("-----------------------");
for (int i = 0; i < x; i++)
{
a[i] = [Link]([Link]());
}
int t;
for (int i = 0; i <= [Link]; i++)
{
for (int j = i+1; j <= [Link] - 1; j++)
{
if (a[i] > a[j])
{
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
[Link]("The Sorted array");
for(int i=0;i<[Link];i++) //writting array
[Link](a[i]+ " ");
[Link]();
}
}
}
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];
[Link]("\n\nFind the second largest element in an array :\n");
[Link]("-----------------------------------------\n");
[Link]("Input the size of array : ");
n = Convert.ToInt32([Link]());
/* Stored values into the array*/
[Link]("Input {0} elements in the array :\n",n);
for(i=0;i<n;i++)
{
[Link]("element - {0} : ",i);
arr1[i] = Convert.ToInt32([Link]());
}
/* find location of the largest element in the array */
lrg=0;
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];
}
}
}
[Link]("The Second largest element in the array is : {0} \n\n", lrg2nd);
}
}
Output:
10. Write program in C# to demonstrate boxing and unboxing.
using System;
using [Link];
using [Link];
using [Link];
namespace BoxUnbox
{
class TestBoxingUnboxing
{
public static void Main()
{
int i = 123;
object obj = i; // IMPLICIT BOXING
[Link]("nThe value-type value = {0}", i);
[Link]("nThe object-type value = {0}", obj);
// Change the contents of i
i = 456;
[Link]("nThe value-type value = {0}", i);
[Link]("nThe object-type value = {0}", obj);
// Assign obj value to other local variable
int j = (int)obj; // UNBOXING
[Link]("nThe value-type value = {0}", j);
[Link]();
}
}
}
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
11. Write a program in C# to demonstrate the usage of object and class.
Using Single Class:
using System;
using [Link];
using [Link];
using [Link];
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
public static void Main( ) //static member function
{
COProgram obj1 = new COProgram( ); // Object creation of Class COProgram
[Link](“ The value of non-static field of a is: {0}”, obj1.a);
[Link](“ The value of static field of b is :{0}”, COProgram.b);
[Link]();
}
}
}
Output:
The value of non-static field of a is: 0
The value of static field of b is: 0
Using Two Class: (Independent class)
using System;
using [Link];
using [Link];
using [Link];
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
void fun( ) //non-static user defined member function of class A
{
[Link](“Function of Class A is called”);
}
}
class B
{
public static void Main()
{
A obj1 = new A( ); // Object creation for Class A
[Link](“ The value of non-static field of x which is data member of
Class A is: {0}”, obj1.x);
[Link](“ The value of static field of y which is data member of Class
A is :{0}”, A.y);
// Calling member fuction of Class A through object of Class A from Class B environment.
[Link]();
[Link]();
}
}
}
Output:
The value of non-static field of x which is data member of Class A is: 0
The value of static field of y which is data member of Class A is : 0
Function of Class A is called.