C#.Net Lab Record Programs
C#.Net Lab Record Programs
Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Palindrome
{
class Program
{
static void Main(string[] args)
{
int n, r, sum = 0, temp;
Console.Write("Enter the Number: ");
n = int.Parse(Console.ReadLine());
temp = n;
while (n > 0)
{
r = n % 10;
sum = (sum * 10) + r;
n = n / 10;
}
if (temp == sum)
Console.Write("Number is Palindrome.");
else
Console.Write("Number is not Palindrome");
Console.ReadLine();
}
}
}
Output:
Aim:
2. Write a program to check whether a number is Prime or not.
Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Prime
{
class Program
{
static void Main(string[] args)
{
int n, i, m = 0, flag = 0;
Console.Write("Enter the Number to check Prime: ");
n = int.Parse(Console.ReadLine());
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
Console.Write("Number is not Prime.");
flag = 1;
break;
}
}
if (flag == 0)
Console.Write("Number is Prime.");
Console.ReadLine();
}
}
}
Output:
Aim:
3. Write a program to demonstrate Command line arguments processing.
Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// Command line arguments
Console.WriteLine("Argument length: " + args.Length);
Console.WriteLine("Supplied Arguments are:");
foreach (Object obj in args)
{
Console.WriteLine(obj);
}
}
}
}
Output:
Aim:
4. Write a program to find the largest and second largest element in a single dimensional
array.
Program Code:
namespace Largest
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Enter the size of the array: ");
int size = int.Parse(Console.ReadLine());
Program Code:
using System;
namespace RectArray
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows & columns for the 1st matrix: ");
int r1 = int.Parse(Console.ReadLine());
int c1 = int.Parse(Console.ReadLine());
int[,] m1 = new int[r1,c1];
Console.Write("Enter the number of rows & columns for the 2nd matrix: ");
int r2 = int.Parse(Console.ReadLine());
int c2 = int.Parse(Console.ReadLine());
int[,] m2 = new int[r1,c1];
if (c1 != r2)
{
Console.WriteLine("The number of column in m1 not equal to number of row in
m2");
return;
}
Program Code:
using System;
namespace JaggedArray
{
internal class Program
{
static void Main(string[] args)
{
int[][] jaggedArray = new int[3][];
int sum = 0;
foreach (int[] innerArray in jaggedArray)
foreach (int element in innerArray)
sum += element;
Program Code:
namespace StringManip
{
internal class Program
{
static void Main(string[] args)
{
string str1 = "Hello World!";
string str2 = " Welcome to C# Programming! ";
// 1. Length
Console.WriteLine("Length of str1: " + str1.Length);
// 2. ToUpper
Console.WriteLine("str1 in uppercase: " + str1.ToUpper());
// 3. ToLower
Console.WriteLine("str1 in lowercase: " + str1.ToLower());
// 4. Concat
Console.WriteLine("Concatenated string: " + String.Concat(str1, str2));
// 5. IndexOf
Console.WriteLine("Index of 'World' in str1: " + str1.IndexOf("World"));
// 6. Substring
Console.WriteLine("Substring of str1: " + str1.Substring(0, 5));
// 7. Replace
Console.WriteLine("Replaced string: " + str1.Replace("Hello", "Hi"));
// 8. Trim
Console.WriteLine("Trimmed string: " + str2.Trim());
// 9. Split
string[] words = str2.Split(' ');
Console.WriteLine("Words in str2:");
foreach (string word in words)
{
Console.WriteLine(word);
}
// 10. StartsWith
Console.WriteLine("str1 starts with 'Hello': " + str1.StartsWith("Hello"));
// 11. EndsWith
Console.WriteLine("str1 ends with 'World!': " + str1.EndsWith("World!"));
// 12. Contains
Console.WriteLine("str1 contains 'llo': " + str1.Contains("llo"));
// 13. Compare
Console.WriteLine("str1 compared to 'hello world': " + String.Compare(str1, "hello
world"));
// 14. Equals
Console.WriteLine("str1 equals 'Hello World!': " + str1.Equals("Hello World!"));
// 15. IsNullOrEmpty
Console.WriteLine("str1 is null or empty: " + String.IsNullOrEmpty(str1));
// 16. IsNullOrWhiteSpace
Console.WriteLine("str2 is null or whitespace: " + String.IsNullOrWhiteSpace(str2));
// 17. PadLeft
Console.WriteLine("str1 padded on the left: " + str1.PadLeft(15));
// 18. PadRight
Console.WriteLine("str1 padded on the right: " + str1.PadRight(15));
// 19. Insert
Console.WriteLine("str1 with 'Hey ': " + str1.Insert(0, "Hey "));
// 20. Remove
Console.WriteLine("str1 with 'World' removed: " + str1.Remove(6, 5));
}
}
}
Output:
Aim:
8. Write a program to demonstrate different types of constructors.
Program Code:
namespace constructors
{
class Person
{
public string Name;
public int Age;
//default constructor
public Person()
{
Name = "Unknown";
Age = 0;
}
// Constructor with parameters
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Copy constructor
public Person(Person other)
{
Name = other.Name;
Age = other.Age;
}
}
internal class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
Console.WriteLine("Name: "+p1.Name+", Age: "+p1.Age);
Program Code:
namespace Inheritance
{
class Vehicle
{
public string Brand;
public string Model;
public int Year;
public void Start()
{
Console.WriteLine("Vehicle started.");
}
public void Stop()
{
Console.WriteLine("Vehicle stopped.");
}
}
vehicle.Start();
car.Accelerate();
motorcycle.Wheelie();
suv.OffRoad();
Console.ReadLine();
}
}
}
Output:
Aim:
10. Write a program to demonstrate interface.
Program Code:
using System;
namespace Interface
{
interface IPolygon
{
void calcArea(int l, int b);
}
class Rectangle: IPolygon
{
public void calcArea(int l, int b)
{
int area = l * b;
Console.WriteLine("Area of Rectangle: " + area);
}
}
internal class Program
{
static void Main(string[] args)
{
Rectangle r1= new Rectangle();
Console.Write("Enter Length: ");
int l = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Breadth: ");
int b = Convert.ToInt32(Console.ReadLine());
r1.calcArea(l, b);
}
}
}
Output:
Aim:
11. Demonstrate arrays of interface types with a C# program.
Program Code:
using System.Transactions;
namespace InterfaceTypes
{
interface IShape
{
double CalArea();
}
Console.WriteLine("Areas of shapes:");
foreach (IShape shape in shapes)
Console.WriteLine(shape.CalArea());
}
}
}
Output:
Aim:
12. Write a program to demonstrate Operator overloading.
Program Code:
namespace OperatorOverloading
{
class Calculator
{
public int num;
public Calculator(){
num = 0;
}
public Calculator(int num1){
this.num = num1;
}
public static Calculator operator +(Calculator c1,Calculator c2)
{
Calculator c3 = new Calculator();
c3.num = c1.num +c2.num;
return c3;
}
public static Calculator operator -(Calculator c1, Calculator c2)
{
Calculator c3 = new Calculator();
c3.num = c1.num - c2.num;
return c3;
}
public void display()
{
Console.WriteLine(num);
}
}
internal class Program{
static void Main(string[] args){
Console.Write("Enter number 1: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter number 2: ");
int num2 = int.Parse(Console.ReadLine());
Calculator c1 = new Calculator(num1);
Calculator c2 = new Calculator(num2);
Calculator c3,c4;
c3 = c1 + c2;
Console.Write("Addition: ");
c3.display();
c4 = c2 - c1;
Console.Write("Subtraction: ");
c4.display();
}
}
}
Output:
Aim:
13. Write a program to demonstrate delegates.
Program Code:
using System;
namespace Delegates
{
internal class Program
{
public delegate void addnum(int a,int b);
public delegate void subnum(int a, int b);
del1(a, b);
del2(a, b);
}
}
}
Output:
Aim:
14. Write a program to demonstrate events.
Program Code:
using System;
namespace Events
{
class Calculator
{
public delegate void Cal(int result);
public event Cal CalCompleted;
public void Add(int num1, int num2)
{
int result = num1 + num2;
CalCompleted?.Invoke(result);
}
}
internal class Program
{
static void Main(string[] args)
{
Calculator cal = new Calculator();
cal.CalCompleted += Complete;
Console.Write("Enter number 1: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter number 2: ");
int num2 = int.Parse(Console.ReadLine());
cal.Add(num1,num2);
}
private static void Complete(int result)
{
Console.WriteLine("Calculation completed. Result: " + result);
}
}
}
Output:
Aim:
15. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.
Program Code:
namespace ExceptionHandling
{
class DivByZero : Exception
{
public DivByZero()
{
Console.WriteLine("Exception has occurred: ");
}
}
internal class Program
{
public double DivisionOperation(double num,double den)
{
if (den== 0)
throw new DivByZero();
return num/den;
}
Program Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace registration
{
public partial class Form1 : Form
{
private bool rb;
private bool cb;
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
if (radioButton1.Checked != false || radioButton2.Checked != false)
rb = true;
if (checkBox1.Checked != false || checkBox2.Checked != false || checkBox3.Checked
!= false || checkBox4.Checked != false)
cb = true;
if (textBox1.Text != "" && textBox2.Text != "" && (rb) &&
dateTimePicker1.Checked != false && textBox3.Text != "" && (cb))
MessageBox.Show("Registration was successfully");
else
MessageBox.Show("Please enter all fields");
}
Program Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace menucontrol
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Output:
Aim:
18. Write a program to create windows database application.
Program Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
OleDbConnection con = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=D:\Programming\Visual Programming Projects\Record\18.
WindowsFormsApplication7\dbapp.accdb");
//OleDbConnection con = new OleDbConnection(@"Provider=OraOLEDB.Oracle;Data
Source=XE;User ID=C##fossbin;Password=oracle;");
int count = 0;
public Form1()
{
InitializeComponent();
}
}
}
Output:
Aim:
19. Write a program to create web database application.
Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=D:\Programming\Visual Programming Projects\Record\19.
WebApplication1\Database11.accdb");
//OleDbConnection con = new OleDbConnection(@"Provider=OraOLEDB.Oracle;Data
Source=XE;User ID=C##fossbin;Password=oracle;");
Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace validation
{
public partial class validation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RangeValidator1.MinimumValue = DateTime.Now.AddYears(-
45).ToShortDateString();
RangeValidator1.MaximumValue = DateTime.Now.AddYears(-
18).ToShortDateString();
}