INDEX
S.NO PROGRAMS DATE SIGN.
1. Solve simple problem using the
fundamental syntax and semantics
of the C# programming language
2. WAP in C# to demonstrate
Command line arguments
processing.
3. WAP in C# to implement stack
memory operations.
4. Write C# program that use
selection (if, switch, conditional
operator)
5. Write C# program that use loops
(while, do while, for)
6. Write a program to reverse a given
string and number using C#.
7. Write C# programs that reads and
prints One-dimensional arrays.
8. Write a C# program to search an
element using Binary Search.
9. Write a C# program to sort array
of elements using Bubble sort.
10. Write a Program in C# to find the
second largest element from Single
dimensional array.
11. Write program in C# to
demonstrate boxing and unboxing.
12. Write a program in C# to
demonstrate the usage of object
and class.
PRACTICAL : 1
AIM: Solve simple problem using the fundamental syntax and
semantics of the C# programming language"
using System;
class Program
{
static void Main()
{
Console.Write("Enter marks: ");
int marks = Convert.ToInt32(Console.ReadLine());
string result = (marks >= 40) ? "Pass" : "Fail";
Console.WriteLine("Result: " + result);
}
}
PRACTICAL : 2
AIM: Write a Program in C# to demonstrate Command line
arguments processing.
using System;
class Program {
static void Main(string[] args) {
foreach (string arg in args) {
Console.WriteLine("Argument: " + arg);
}
}
}
PRACTICAL : 3
AIM: Write a program in c# to implement stack memory
operations.
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
Console.WriteLine("Popped: " + stack.Pop());
Console.WriteLine("Peek: " + stack.Peek());
}
}
PRACTICAL : 4
AIM: Write C# programs that use selection (if, switch, conditional
operator)
using System;
class Program {
static void Main() {
int num = 10;
// if
if (num > 0) Console.WriteLine("Positive");
// switch
switch (num) {
case 10: Console.WriteLine("Ten"); break;
default: Console.WriteLine("Other"); break;
}
// conditional
string result = (num % 2 == 0) ? "Even" : "Odd";
Console.WriteLine(result);
} }
PRACTICAL : 5
AIM: Write C# program that use loops (while, do while, for)
using System;
class Program {
static void Main() {
// for loop
for (int i = 0; i < 3; i++) {
Console.WriteLine("For: " + i);
}
// while loop
int j = 0;
while (j < 3) {
Console.WriteLine("While: " + j);
j++;
}
// do-while loop
int k = 0;
do {
Console.WriteLine("Do-While: " + k);
k++;
} while (k < 3);
}
}
PRACTICAL: 6
AIM: Write a program to reverse a given string and number
using C# .
using System;
class Program {
static void Main() {
string str = "hello";
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
Console.WriteLine("Reversed string: " + new string(charArray));
int num = 12345;
int rev = 0;
while (num > 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
Console.WriteLine("Reversed number: " + rev);
}
}
PRACTICAL: 7
AIM: Write C# programs that reads and prints One-dimensional
arrays.
using System;
class Program {
static void Main() {
int[] arr = {1, 2, 3, 4, 5};
foreach (int val in arr) {
Console.WriteLine(val);
}
}
}
PRACTICAL: 8
AIM: Write a C# program to search an element using Binary
Search.
using System;
class Program {
static void Main() {
int[] arr = {10, 20, 30, 40, 50};
int key = 30;
int index = Array.BinarySearch(arr, key);
Console.WriteLine(index >= 0 ? $"Found at index {index}" : "Not
found");
}
}
PRACTICAL: 9
AIM: Write a C# program to sort array of elements using Bubble
sort.
using System;
class Program {
static void Main() {
int[] arr = {5, 1, 4, 2, 8};
for (int i = 0; i < arr.Length - 1; i++) {
for (int j = 0; j < arr.Length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
foreach (int val in arr) Console.Write(val + " ");
}
}
PRACTICAL: 10
AIM: Write a Program in C# to find the second largest element
from Single dimensional array.
using System;
class Program {
static void Main() {
int[] arr = {10, 20, 4, 45, 99};
int first = int.MinValue, second = int.MinValue;
foreach (int n in arr) {
if (n > first) {
second = first;
first = n;
} else if (n > second && n != first) {
second = n;
}
}
Console.WriteLine("Second largest: " + second);
}
}
PRACTICAL: 11
AIM: Write program in C# to demonstrate boxing and unboxing.
using System;
class Program {
static void Main() {
int num = 123;
object obj = num; // Boxing
int unboxed = (int)obj; // Unboxing
Console.WriteLine("Boxed: " + obj);
Console.WriteLine("Unboxed: " + unboxed);
}
}
AIM: Write a program in C# to demonstrate the usage of object
and class.
using System;
class Person {
public string Name;
public void SayHello() {
Console.WriteLine("Hello, " + Name);
}
}
class Program {
static void Main() {
Person p = new Person();
p.Name = "Aditya";
p.SayHello();
}
}