0% found this document useful (0 votes)
708 views18 pages

Dot Net LAb Manual - 5th Sem BCA

The document contains a lab manual for a .NET programming lab course. The summaries are: 1) The lab manual contains 6 exercises for students to complete that involve fundamental C# syntax, command line arguments, stack memory operations, conditional statements, loops, and reversing strings and numbers. 2) The exercises include writing simple C# programs to demonstrate basic concepts like variable declaration, input/output, and arithmetic operations. 3) More advanced exercises involve programming concepts like selection statements, loops, parameter passing, and reversing strings and numbers. The exercises are designed to help students learn and practice core C# programming skills.

Uploaded by

Priyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
708 views18 pages

Dot Net LAb Manual - 5th Sem BCA

The document contains a lab manual for a .NET programming lab course. The summaries are: 1) The lab manual contains 6 exercises for students to complete that involve fundamental C# syntax, command line arguments, stack memory operations, conditional statements, loops, and reversing strings and numbers. 2) The exercises include writing simple C# programs to demonstrate basic concepts like variable declaration, input/output, and arithmetic operations. 3) More advanced exercises involve programming concepts like selection statements, loops, parameter passing, and reversing strings and numbers. The exercises are designed to help students learn and practice core C# programming skills.

Uploaded by

Priyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

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 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.

Console.WriteLine("Hello ! Welcome to C# Programming Language”);


Console.WriteLine( “----------------------------------------------------------“);

/* 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

2. Write a Program in C# to demonstrate Command line arguments processing.

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

Number of CommadLine Arguments: 2

CommandLine Arguments are: REVA UNIVERSITY

3. Write a program in c# to implement stack memory operations

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);
}

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
System.Console.WriteLine( “The sum of two number is: {0}”,result);
System.Console.ReadLine();
}
}
}

Output:

The sum of two number is: 9


4. Write C# programs that use selection (if, switch, conditional operator)

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());

if(ch =='A'|| ch =='a'){


value = num1 + num2;
Console.WriteLine("Addition of {0} and {1} is {2}", num1, num2, value);
}
else if(ch =='S' || ch=='s'){
value = num1-num2;
Console.WriteLine("Subtraction of {0} and {1} is {2}", num1, num2, value);
}
else if(ch=='M'|| ch =='m'){
value = num1 * num2;
Console.WriteLine("Multiplication of {0} and {1} is {2}", num1, num2, value);
}
else if (ch == 'D' || ch == 'd'){
value=num1/num2;
Console.WriteLine("Division of {0} and {1} is {2}", num1, num2, value);
}
else{
Console.WriteLine("Please enter either A/a for addition, S/s for subtraction, M/m for
multiplication and D/d for division");
}
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());

Console.WriteLine("Enter an Arithmetic Operator (+,-,*,/) ");


ch = Console.ReadLine()[0];
// ch=Convert.ToChar(Console.ReadLine());

switch (ch)
{

case '+': value = num1 + num2;


Console.WriteLine("Addition of {0} and {1} is {2}", num1, num2, value);
Console.ReadLine();
break;

case '-': value = num1 - num2;


Console.WriteLine("Subtraction of {0} and {1} is {2}", num1, num2, value);
Console.ReadLine();
break;
case '*':
value = num1 * num2;
Console.WriteLine("Multiplication of {0} and {1} is {2}", num1, num2, value);
Console.ReadLine();
break;
case '/': value = num1 / num2;
Console.WriteLine("Division of {0} and {1} is {2}", num1, num2, value);
Console.ReadLine();
break;
default: Console.WriteLine("Please enter either + for addition, - for subtraction, *
for multiplication and / for division");
Console.ReadLine();
break;
}

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:

5. Write C# programs that use loops (while, do while, for)

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:

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 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:

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];

Console.Write("\n\nRead and Print elements of an array:\n");


Console.Write("-----------------------------------------\n");

// Read the Array elements


Console.Write("Input 10 elements in the array :\n");
for(i=0; i<10; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}

// Display the Array elements


Console.Write("\nElements in array are: ");
for(i=0; i<10; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\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];
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:

8. Write a C# program to sort array of elements using Bubble sort.

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];

Console.Write("\n\nFind the second largest element in an array :\n");


Console.Write("-----------------------------------------\n");

Console.Write("Input the size of array : ");


n = Convert.ToInt32(Console.ReadLine());
/* Stored values into the array*/
Console.Write("Input {0} elements in the array :\n",n);
for(i=0;i<n;i++)
{
Console.Write("element - {0} : ",i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
/* 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];
}
}
}

Console.Write("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 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);

// Assign obj value to other local variable


int j = (int)obj; // UNBOXING
Console.WriteLine("nThe value-type value = {0}", j);
Console.ReadLine();
}
}
}

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 System.Collections.Generic;
using System.Linq;
using System.Text;

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
System.Console.WriteLine(“ The value of non-static field of a is: {0}”, obj1.a);
System.Console.WriteLine(“ The value of static field of b is :{0}”, COProgram.b);
System.Console.ReadLine();
}
}
}

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 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

void fun( ) //non-static user defined member function of class A


{
System.Console.WriteLine(“Function of Class A is called”);
}
}
class B
{
public static void Main()
{
A obj1 = new A( ); // Object creation for Class A
System.Console.WriteLine(“ The value of non-static field of x which is data member of
Class A is: {0}”, obj1.x);
System.Console.WriteLine(“ 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.
obj1.fun();
System.Console.ReadLine();
}
}
}
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.

You might also like