0% found this document useful (0 votes)
3K views

Experiment-1: Write A Program Using Function Overloading To Swap Two Integer Numbers and Two Float Numbers

The document describes an experiment to implement a traffic signal simulation using delegates in C#. It defines a TrafficDel delegate that can refer to the Yellow(), Green(), and Red() methods of the TrafficSignal class. These methods print the meaning of each light color. The TrafficSignal class initializes an array of TrafficDel delegates to each method. It then calls the show() method, which invokes each delegate in the array to simulate the light sequence.

Uploaded by

Varun Vaid
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Experiment-1: Write A Program Using Function Overloading To Swap Two Integer Numbers and Two Float Numbers

The document describes an experiment to implement a traffic signal simulation using delegates in C#. It defines a TrafficDel delegate that can refer to the Yellow(), Green(), and Red() methods of the TrafficSignal class. These methods print the meaning of each light color. The TrafficSignal class initializes an array of TrafficDel delegates to each method. It then calls the show() method, which invokes each delegate in the array to simulate the light sequence.

Uploaded by

Varun Vaid
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

EXPERIMENT-1

AIM- Write a program using Function Overloading to swap two integer


numbers and
two float numbers.

CODE
using System;
namespace swap
{
class Overloading
{
public void swap(ref int a, ref int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
public void swap(ref float f1, ref float f2)
{
float f;
f=f1;
f1=f2;
f2=f;
}
}
class program
{
static void Main(string[] args)
{
Overloading i=new Overloading();
int a=2, b=100;
Console.WriteLine("BEFORE SWAPPING\t A="+ a + "\tB=" + b);
i.swap(ref a, ref b);
Console.WriteLine("AFTER SWAPPING\t A="+ a + "\tB=" + b);
float f1=10.5f,f2=0.4f;
Console.WriteLine("BEFORE SWAPPING\t A="+ f1 + "\tB=" + f2);
i.swap(ref f1,ref f2);
Console.WriteLine("AFTER SWAPPING \t F1=" + f1 + "\tF2="+ f2);
}
}
}
OUTPUT:
EXPERIMENT-2
AIM: Write a program to accept a number from the user and
throw an exception if the number is not an even number

CODE:
using System;
public class Custom : Exception
{
}
public class TestException
{
public static void Main()
{
int num;
Console.WriteLine("Enter a number : ");
num = int.Parse(Console.ReadLine());
try{
if(num % 2 == 0)
Console.WriteLine("No exception is found");
else
throw new Custom();
}
catch(Custom e)
{
Console.WriteLine("Exception Occured : Number
isn't an even number \t" + e.Message);
}
}
}

OUTPUT:
EXPERIMENT-3
AIM- Write a program to display addition of two numbers using
windows application

CODE-
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//submit button coding


private void button1_Click(object sender, EventArgs e)
{
int a, b, c;
a = Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
c = a + b;
textBox3.Text = c.ToString();
}
}
}

OUTPUT:
EXPERIMENT-4

AIM : Write a program to make a simple calculator using


windows application.

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
double FirstNumber;
string Operation;
public Form1()
{
InitializeComponent();
}
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + '.';
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 0;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 1;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 2;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 3;
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 4;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 5;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 6;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 7;
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 8;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 9;
}
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void button11_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "+";
}
private void button12_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "-";
}
private void button13_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "*";
}
private void button14_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "/";
}
private void button15_Click(object sender, EventArgs e)
{
double SecondNumber;
double Result;
SecondNumber = Convert.ToDouble(textBox1.Text);
if (Operation == "+")
{
Result = (FirstNumber + SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "-")
{
Result = (FirstNumber - SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "*")
{
Result = (FirstNumber * SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "/")
{
if (SecondNumber == 0)
{
textBox1.Text = "Cannot divide by zero";
}
else
{
Result = (FirstNumber / SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
}
}
}
}
Experiment -5
Aim
Write a program to implement MDI winform in c#.

Code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
IsMdiContainer = true;
}
private void menu1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.MdiParent = this;
}
}
}
Output:
Experiment-6
Aim: To implement operator overloading in csharp
Code
class Calculate
{
public int number1, number2;
public Calculate ( int no1, int no2)
{
number1 = no1;
number2 = no2;
}
public Calculate()
{
}
public static Calculate operator – ( Calculate c1 )
{
c1.number1 = -c1.number1;
c1.number2 = -c1.number2;
return c1;
}
public void Print()
{
Console.WriteLine(“number1=” + number1);
Console.WriteLine(“number2 =” + number2);
Console.Read();
}
class Program
{
static void Main ( string[ ] args )
{
Calculate c = new Calculate(20, -40);
c.Print();
Calculate c1 = new Calculate();
c1 = -c;
c1.Print();
Console.Read();
}
}
}
Output:
Experiment-7

Aim - To implement a student database.


Software Used - Visual Studio
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.SqlClient;
namespace StudentProgram{
public partial class Form1 : Form{
SqlConnection con = new SqlConnection(@"Data
Source=Admin\SQL;InitialCatalog=STUDENT;Integrated Security=True");
SqlCommand cmd;
SqlDataAdapter adpt;
DataTable dt;
public Form1(){
InitializeComponent();
}
private void label4_Click(object sender, EventArgs e){
}
private void Form1_Load(object sender, EventArgs e){
private void sbtstudent_Click(object sender, EventArgs e){
con.Open();
SqlCommand cmd = new SqlCommand("insert into Student values('"
+roll.Text + "', '" +sname.Text + "', '" + age.Text + "', '" + scid.Text + "', '" +
fees.Text + "')",con);
cmd.ExecuteNonQuery();
MessageBox.Show("Student details saved");
}
private void Acdcourse_Click(object sender, EventArgs e){
con.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("select * from Student
DataTable dtbl = new DataTable();
sqlda.Fill(dtbl);
dgv1.DataSource = dtbl;
}
}

OUTPUT
EXPERIMENT 8
AIM: Write a program to delegate called TrafficDel and a class
called TrafficSignal with a following delegate methods : Yellow(),
Green() and Red(). Also include a method IdentifySignal() to initialize
an array of delegate with the above methods and a method Show()
to invoke members of the above array.

CODE
using System;

public delegate void TrafficDel();


class TrafficSignal
{
public static void Yellow()
{
Console.WriteLine("Yellow light signals to get ready");
}
public static void Green()
{
Console.WriteLine("Green light signals to go");
}
public static void Red()
{
Console.WriteLine("Red light signals to stop");
}
TrafficDel[] td = new TrafficDel[3];
public void IdentifySignal()
{
td[0] = new TrafficDel(Yellow);
td[1] = new TrafficDel(Green);
td[2] = new TrafficDel(Red);
}
public void show()
{
td[0]();
td[1]();
td[2]();
}
}
class Program
{
static void Main(string[] args)
{
TrafficSignal ts = new TrafficSignal();
ts.IdentifySignal();
ts.show();
}}

You might also like