Sei sulla pagina 1di 10

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT

C#- LAB MANUAL SIMPLE IF using System; using System.Collections.Generic; using System.Text; namespace ex1 { class Program { static void Main(string[] args) { Console.WriteLine("enter the number"); int n = Int32.Parse(Console.ReadLine()); If(n>0) Console.WriteLine("+ve"); Console.ReadLine(); } } } OUTPUT: enter the number 4 +ve IF ELSE using System; using System.Collections.Generic; using System.Text; namespace ex13 { class Program { static void Main(string[] args) { Console.WriteLine("enter the number"); int n = Int32.Parse(Console.ReadLine()); if (n > 0) Console.WriteLine("+ve"); else Console.WriteLine("-ve"); Console.ReadLine(); } } } OUTPUT enter the number 5 +ve enter the number -5 -ve ELSE IF using System;

using System.Collections.Generic; using System.Text; namespace ex2 { class Program { static void Main(string[] args) { Console.WriteLine("enter the number"); int n = Int32.Parse(Console.ReadLine()); if (n >= 0 && n <= 9) Console.WriteLine("single digit"); else if (n > 10 && n <= 99) Console.WriteLine("double digit"); else Console.WriteLine("any digit"); Console.ReadLine(); } } } OUTPUT enter the number 4 single digit enter the number 66 double digit enter the number 777 any digit NESTED IF using System; using System.Collections.Generic; using System.Text; namespace ex3 { class Program { static void Main(string[] args) { Console.WriteLine("enter the month"); string s = Console.ReadLine(); Console.WriteLine("enter the year"); int yy = Int32.Parse(Console.ReadLine()); if (s=="february") { Console.WriteLine("this month is february"); if (yy % 4 == 0) Console.WriteLine("leap year");

Page 1

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT else { Console.WriteLine("not leap year"); } } else Console.WriteLine("any month"); Console.ReadLine(); } } } OUTPUT enter the month february enter the year 2010 this month is february not leap year enter the month march enter the year 2010 any month SWITCH using System; using System.Collections.Generic; using System.Text; namespace ex4 { class Program { static void Main(string[] args) { Console.WriteLine("enter the symbol"); char sym = char.Parse(Console.ReadLine()); switch (sym) { case'+': Console.WriteLine("add"); break; case'-': Console.WriteLine("sub"); break; case'*': Console.WriteLine("mul"); break; } Console.ReadLine(); } } } OUTPUT enter the symbol + Add WHILE using System; using System.Collections.Generic; using System.Text; namespace ex5 { class Program { static void Main(string[] args) { Console.WriteLine("enter the table"); int num = Int32.Parse(Console.ReadLine()); int i = 1; while (i<3) { Console .WriteLine (num+"*"+i+"="+(num*i)); i++; Console.ReadLine(); } } } } OUTPUT enter the table 3 3*1=3 3*2=6 3*3=9 DO WHILE using System; using System.Collections.Generic; using System.Text; namespace ex6 { class Program { static void Main(string[] args) { Console.WriteLine("enter the table"); int num = Int32.Parse(Console.ReadLine()); int i = 1; do { Console.WriteLine(num + "*" + i + "="+(num * i)); i++; Console.ReadLine(); } while (i <= 3); } } } OUTPUT enter the table 5 5*1=5 5*2=10 5*3=15 FOR using System; using System.Collections.Generic; using System.Text; namespace ex7 { class Program { static void Main(string[] args)

Page 2

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT { for (int i = 1; i <= 5; i++) { Console.WriteLine(i); Console.ReadLine(); } } } } OUTPUT 12345 NESTED FOR using System; using System.Collections.Generic; using System.Text; namespace ex8 { class Program { static void Main(string[] args) { for (int i = 1; i <= 2; i++) { for (int j = 11; j <= 12; j++) { Console.WriteLine(j + ""); Console.WriteLine(); Console.ReadLine(); } } } }} OUTPUT 11 12 11 12 ARRAY (A collection of data elements arranged to be indexed in one or more dimensions) using System; using System.Collections.Generic; using System.Text; namespace ex9 { class Program { static void Main(string[] args) { int[] a = new int[5]; for (int i = 0; i < 3; i++) { a[i] = Int32.Parse(Console.ReadLine()); } for (int i = 0; i < 3; i++) Console.WriteLine(a[i]); Console.ReadLine(); } } } OUTPUT 123123 CLASS AND OBJECTS using System; using System.Collections.Generic; using System.Text; namespace ex10 { class first { public void aa() { Console.WriteLine("class and object"); } public void bb() { Console.WriteLine("c sharp"); } } class Program { static void Main(string[] args) { first f = new first(); f.aa(); f.bb(); Console.ReadLine(); } } } OUTPUT class and object c sharp CONSTRUCTOR AND DESTRUCTOR Constructor:(A special member function for automatically creating an instance of a class. Same class and function name) Destructor:(Used to destroy the memory space) using System; using System.Collections.Generic; using System.Text; namespace ex11 { class first { public first() { Console .WriteLine ("empty constructor"); } public first(int a) { Console .WriteLine ("parameterized constructor:"+a); } public first (string s1,string s2) { Console .WriteLine ("string1:"+s1); Console .WriteLine ("string2:"+s2); Console .WriteLine ("join:"+(s1+s2)); } ~first() { Console .WriteLine ("destructor"); } class Program

Page 3

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT { static void Main(string[] args) { new first(); new first(10); new first("c sharp", "language"); Console.ReadLine(); } } } } OUTPUT empty constructor parameterized constructor:10 string1:c sharp string2:language join:c sharplanguage INHERITANCE (Using child class we call the parent class) using System; using System.Collections.Generic; using System.Text; namespace ex12 { class first { public int num = 10; public void get() { Console.WriteLine("number:" + num); } } class second : first { public int a = 100; public void child() { Console.WriteLine("child class:" + a); } } class Program { static void Main(string[] args) { second s=new second (); s.get (); s.child (); Console.ReadLine(); } } } OUTPUT number:10 child class: 100 OVERRIDING INHERITANCE using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication19 { class first { public virtual void get() { Console.WriteLine("it is a parent class"); } } class second:first { public override void get() { base.get(); Console.WriteLine("child class"); } } class Program { static void Main(string[] args) { second s = new second(); s.get(); System.Console.ReadLine(); } } } OUTPUT: it is a parent class child class EXCEPTION HANDLING:(Runtime error handling) Eg1: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication20 { class Program { static void Main(string[] args) { try { int a = 10; int b = 0; int c = a / b; Console.WriteLine(c); } catch (DivideByZeroException d) { Console.WriteLine("error:" + d.Message); } Console.WriteLine("thanks u"); System.Console.ReadLine(); } }

Page 4

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT } OUTPUT: error:Attempted to divide by zero. thanks u TO CHECK THE USER NAME AND PASSWORD using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication16 { class Program { static void Main(string[] args) { string un, pwd; System.Console.WriteLine("ENTER THE user name"); un = System.Console.ReadLine(); System.Console.WriteLine("ENTER THE PASS WORD"); pwd = System.Console.ReadLine(); if ((un == "admin") && (pwd == "soft")) { System.Console.WriteLine("u' r entered the correct USERNAME PASSWORD"); } else { System.Console.WriteLine("u' r entered the wrong USERNAME PASSWORD"); } System.Console.ReadLine(); } } } OUTPUT: ENTER THE user name ADMIN ENTER THE PASS WORD SOFT u' r entered the wrong USERNAME PASSWORD PROGRAM TO CHECK SINGLE DIGIT OR DOUBLE AND MORE using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication18 { class Program { static void Main(string[] args) { int n; System.Console.WriteLine("enter the number here"); n= System.Convert.ToInt32(System.Console.ReadLine()); if (n < 10) { System.Console.WriteLine("u'r enter the signal number"); } else if((n>=10)&&(n<100)) { System.Console.WriteLine("u'r enter the double digit number"); } else { System.Console.WriteLine("u'r enter the more than double digit"); } System.Console.ReadLine(); } } } OUTPUT enter the number here 78 u'r enter the double digit number FINDING THE ARTHMETIC OPERATOR FROM CHARACTER using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication24 { class Program { static void Main(string[] args) { char n; System.Console.WriteLine("enter the value here:"); n= System.Convert.ToChar(System.Console.ReadLine()); if ((n == '+') || (n == '-') || (n == '*') || (n == '/') || (n == '%')) { System.Console.WriteLine("u 'r enter the arthmetic values"); } else { System.Console.WriteLine("u' r enter the other char"); } System.Console.ReadLine(); } } }

Page 5

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT n3 = 4 * a * c; System.Console.WriteLine("n3 Value is n3={0}",n3); n4=2*a; System.Console.WriteLine("n4 valueis n4={0}", n4); r1 = (n1 + ((n2 - n3) ^ 1 / 2)); r2 = (n1 - ((n2 - n3) ^ 1 / 2)); rt1 = r1 / n4; rt2 = r2 / n4; System.Console.WriteLine("FIRST VALUE IS R1={0}", r1); System.Console.WriteLine("SECOND VALUE IS R1={0}", r1); System.Console.WriteLine("ROOT ONE VALUE IS R1={0}", rt1); System.Console.WriteLine("ROOT TWO VALUE IS R1={0}", rt2); System.Console.ReadLine(); } } } OUTPUT enter the a b c value 123 n1 value is n1=-2 n2 value is n1=4 n3 Value is n3=12 n4 valueis n4=2 FIRST VALUE IS R1=-10 SECOND VALUE IS R1=-10 ROOT ONE VALUE IS R1=-5 ROOT TWO VALUE IS R1=3 C#WINDOWS PROGRAMMING BUTTON TOOL PROCEDURE: 1. Create a new project. 2. Add 2 buttons , 2 textbox & 2 labels in the form. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WINDOWS1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string us = "pmu"; string ps = "msc"; if ((textBox1.Text == us) && (textBox2.Text == ps)) { System.Windows.Forms.MessageBox.Show("u are enter the correct username and password"); }

OUTPUT enter the value here:+ u 'r enter the arthmetic values CONVERSION OF CHARACTER VALUE TO ASCII VALUE using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication25 { class Program { static void Main(string[] args) { char c; int n; System.Console.WriteLine("enter the character here"); c= System.Convert.ToChar(System.Console.ReadLine()); n = (int)c; System.Console.WriteLine("ascii value is {0}", n); if(((n>=65)&&(n<=92))||(n>=97)&(n<=122)) { System.Console.WriteLine("u' r entered the alphatic value",c); } else if ((n <= 45) && (n >= 57)) { System.Console.WriteLine("u 'r entered the numeric value ya", c); } System.Console.ReadLine(); } } } OUTPUT enter the character here 3 ascii value is 51 QUADRATIC EQUATION using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication26 class Program { static void Main(string[] args) { int a, b, c, n1, n2, n3, n4, r1, r2, rt1, rt2; System.Console.WriteLine("enter the a b c value"); a= System.Convert.ToInt32(System.Console.ReadLine()); b= System.Convert.ToInt32(System.Console.ReadLine()); c= System.Convert.ToInt32(System.Console.ReadLine()); n1 = -b; System.Console.WriteLine("n1 value is n1={0}", n1); n2 = b * b; System.Console.WriteLine("n2 value is n1={0}", n2);

Page 6

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT else { System.Windows.Forms.MessageBox.Show("u'r enter the wrong username and password"); } } private void button2_Click(object sender, EventArgs e) { textBox1 .Text =""; textBox2 .Text =""; } } } OUTPUT: string txt = "'"; if (checkBox1.Checked) txt +="you want floor mats.\n"; if (radioButton1.Checked) txt += "you choosed the lotus,\n"; if (radioButton2.Checked) txt += "you choosed the rose,\n"; if (radioButton3.Checked) txt += "you choosed the lilly ,\n"; label1.Text = txt; } }} OUTPUT:

GROUPBOX,CHECKBOX,RADIOBUTTON TOOLS PROCEDURE: 1. Create a new project. 2. Add a group box to the form. 3. Inside the groupbox place 3 radiobuttons. 4. Add checkbox & button in the form. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Windowsmsc41 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void groupBox1_Enter(object sender, EventArgs e) { groupBox1.Text = "national flower: you are in the group"; } private void groupBox1_leave(object sender, EventArgs e) { groupBox1.Text = "national flower:thanks for visiting the group"; } private void button1_Click(object sender, EventArgs e) {

CHECKEDLISTBOX,LISTBOX,COMBOBOX PROCEDURE: 1. Create a new project. 2. Add a checkedlistbox, listbox, combobox in the form. 3. Place the button. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Windowsmsc42 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string strc = ""; for (int i = 0; i < checkedListBox1.Items.Count; i+ +) { if (checkedListBox1.GetItemChecked(i)) { strc += "radio item"; strc += checkedListBox1.Items[i].ToString(); strc += "\n"; } } label1.Text = strc; if (listBox1.SelectedItem != null) { strc += "car" + listBox1.SelectedItem + "\n"; } label1.Text = strc; if (comboBox1.Text != "")

Page 7

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT { strc += "dealer:" + comboBox1.Text + "\n"; label1.Text = strc; } else { strc += "you didnt choose any dealer!" + "\n"; label1.Text = strc; } } }} OUTPUT } } private void button2_Click(object sender, EventArgs e) { if(colorDialog1.ShowDialog()==DialogResult.OK) { textBox1.BackColor = colorDialog1.Color; } } private void button3_Click(object sender, EventArgs e) { if(openFileDialog1.ShowDialog()==DialogResult.O K) { richTextBox1.LoadFile(openFileDialog1.FileNa me); } } private void button4_Click(object sender, EventArgs e) { if(saveFileDialog1.ShowDialog()==DialogResult.O K) { richTextBox1.SaveFile(saveFileDialog1.FileNam e); } } } OUTPUT:

DIALOG CONTROL PROCEDURE: 1. 2. 3. Create a new project. Place 4 buttons in the form. From the toolbox add the fontdialog, savedialog, colordialog, opendialog in the form

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace dia { public partial class Form1 : Form public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (fontDialog1.ShowDialog() == DialogResult.OK) { textBox1.Font = fontDialog1.Font; { using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsApplication14 C# DATABASE:

Page 8

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT { public partial class Form1 : Form { OleDbConnection con=new OleDbConnection(); OleDbDataAdapter ada=new OleDbDataAdapter(); OleDbCommandBuilder cmd=new OleDbCommandBuilder(); DataTable dt=new DataTable(); DataRow dr; int t,count; string s; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { con.ConnectionString=@"Provider=Microsoft .Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\data.mdb;Persist Security Info=False"; con.Open(); ada=new OleDbDataAdapter("select * from table1",con); cmd=new OleDbCommandBuilder(ada); ada.Fill(dt); dataGridView1.DataSource=dt; } private void button1_Click(object sender, EventArgs e) { int sample; sample=System.Convert.ToInt32(textBox1.Text); s=@"insert into table1 values("+"'"+textBox1.Text+"'"+","+"'"+textBox2.Text+"'" +")"; ada=new OleDbDataAdapter(s,con); cmd=new OleDbCommandBuilder(ada); ada.Fill(dt); } private void button2_Click(object sender, EventArgs e) { s="update table1 set name="+"'"+textBox2.Text+"'"+"where rollno="+System.Convert.ToInt32(textBox1.Text); ada=new OleDbDataAdapter(s,con); cmd=new OleDbCommandBuilder(ada); ada.Fill(dt); } private void button3_Click(object sender, EventArgs e) { s="delete * from table1 where name="+"'"+textBox2.Text+"'"; ada=new OleDbDataAdapter(s,con); cmd=new OleDbCommandBuilder(ada); ada.Fill(dt); } private void button4_Click(object sender, EventArgs e) { textBox1.Text=System.Convert.ToString(dt.R ows[0][0]); textBox2.Text=System.Convert.ToString(dt.Rows[0][1]); } private void button6_Click(object sender, EventArgs e) { t=t+1; if(t<dt.Rows.Count) { textBox1.Text = System.Convert.ToString(dt.Rows[t][0]); textBox2.Text = System.Convert.ToString(dt.Rows[t][1]); } else{ t = 0; textBox1.Text = System.Convert.ToString(dt.Rows[t][0]); textBox2.Text = System.Convert.ToString(dt.Rows[t][1]); } } private void button7_Click(object sender, EventArgs e) { t=t-1; if(t<dt.Rows.Count) { textBox1.Text = System.Convert.ToString(dt.Rows[t][0]); textBox2.Text = System.Convert.ToString(dt.Rows[t][1]); } else { t = dt.Rows.Count - 1; textBox1.Text = System.Convert.ToString(dt.Rows[t][0]); textBox2.Text = System.Convert.ToString(dt.Rows[t][1]); } } private void button8_Click(object sender, EventArgs e) { t = 0; int find, pos; find = 0; pos = 0; while (t < dt.Rows.Count) { if (System.Convert.ToString((dt.Rows[t][0])) == textBox3.Text) { find = 1; pos = t; } t++; } if (find == 1) {

Page 9

DEPARTMENT OF SOFTWARE ENGINEERING STUDENT SOFTWARE DEVELOPMENT textBox1.Text = System.Convert.ToString(dt.Rows[pos][0]); textBox2.Text = System.Convert.ToString(dt.Rows[pos][1]); } } }}

Page 10

Potrebbero piacerti anche