Professional Documents
Culture Documents
a) Write a console application that obtains four int values from the user and
displays the sum of that four values.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n1, n2,n3,n4, sum;
Console.WriteLine("Enter 1st no.");
n1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter 2nd no.");
n2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter 3rd no.");
n3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter 4th no.");
n4 = Convert.ToInt32(Console.ReadLine());
sum = n1 + n2 + n3 + n4;
Console.WriteLine("\n Sum of four int values are {0}", sum);
Console.ReadKey();
}
}
}
Output:
OUTPUT:-
Output:
Output:
Output:
ii. Generate various patterns (triangles, diamond and other patterns) with
numbers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i, j, n;
Console.WriteLine("Enter number");
n = Int32.Parse(Console.ReadLine());
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine("\n");
}
Console.ReadLine();
}
}
}
Output:
Output:
iv.Switch Case
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
public delegate void EventHandler();
class deleg
{
public static event EventHandler display;
static void Main(string[] args)
{
display += new EventHandler(Red);
display += new EventHandler(Green);
display += new EventHandler(Blue);
display += new EventHandler(Yellow);
display.Invoke();
Console.ReadLine();
}
static void Red()
{
Console.WriteLine("Rose is Red");
}
static void Green()
{
Console.WriteLine("Grass is Green");
}
static void Blue()
{
Console.WriteLine("Sky is Blue");
}
static void Yellow()
{
Console.WriteLine("Lemon is Yellow");
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int a ;
char c;
Console.WriteLine("Enter any alphabet:");
a = Console.Read();
c = Convert.ToChar(a);
switch (c)
{
case 'a': Console.WriteLine("a is vowel");
break;
case 'e': Console.WriteLine("e is vowel");
break;
case 'i': Console.WriteLine("i is vowel");
break;
case 'o': Console.WriteLine("o is vowel");
break;
case 'u': Console.WriteLine("u is vowel");
break;
default: Console.WriteLine("it is not vowel");
break;
}
Console.ReadKey();
}
}
}
Output:
Output:
Constructor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class construct
{
int x;
public construct(int a, int b)
{
Console.WriteLine(a + b);
}
public construct(float a, float b)
{
Console.WriteLine(a + b);
}
public construct()
{
x = 2;
Console.WriteLine(x+x);
}
}
class Program
{
static void Main(string[] args)
{
construct c1 = new construct(3, 5);
construct c2 = new construct(12.5f,13.2f);
construct c3 = new construct();
Console.ReadKey();
}
}
}
Output:
Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
interface shape
{
void calculate(float f1, float f2);
}
class circle : shape
{
public void calculate(float x, float y)
{
Console.WriteLine("Circle area:{0}", 3.14f * x * x)
;
}
}
class rectangle : shape
{
public void calculate(float x, float y)
{
Console.WriteLine("rectangle area:{0}",x*y);
}
}
class Program
{
static void Main(string[] args)
{
circle c = new circle();
rectangle r = new rectangle();
shape s;
s = c;
s.calculate(10, 0);
s = r;
s.calculate(10, 10);
Console.ReadKey();
}
}
Output:
Function overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void sum(int a, int b)
{
Console.WriteLine(a + b);
}
static void sum(float a, float b)
{
Console.WriteLine(a + b);
}
static void sum(int a)
{
Console.WriteLine(a + a);
}
static void Main(string[] args)
{
sum(2,3);
sum(12.3f, 2.4f);
sum(4);
Console.ReadKey();
}
}
}
Output:
internalcss.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Page</title>
<style type="text/css">
h2,p{color:Red;font-family:Arial;}
th{text-align:center;background:green;font-weight:bolder;}
td{text-align:center;color:Blue;}</style>
</head>
<body>
<h2>Example of Internal css</h2>
<table
border="1"><tr><th>WST</th><td>CSS</td></tr><tr><th>OOP</th><td>C+
+</td></tr></table>
<p>Embedded css is much better than Inline css. Atleast it doesn't mix up with
HTML tags to confuse us.</p>
</body>
</html>
Output:
Externalcss.html
Tyit.css
body {background:black}
h2,p
{
color:White;
font-family:Arial;
text-align:center;
}
h2
{
text-transform:uppercase;
text-decoration:underline;
}
th
{
text-align:center;
background:green;
font-weight:bolder;
}
td
{
text-align:center;
color:Blue;
}
pi
{
color:White;
border-style:solid;
font-weight:lighter;
}
Output:-
</div>
</form>
</body>
</html>
Coocki
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Cookies.aspx.cs" Inherits="Cookies" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="getcoo"/>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
</div>
</form>
</body>
</html>
Output
onserverclick="srvr_chng" />
</form>
</body>
</html>
Output:
{
int row = 0;
int numr = Convert.ToInt32(selrow.Value);
int numc = Convert.ToInt32(selcol.Value);
for (int j = 0; j < numr; j++)
{
HtmlTableRow r = new HtmlTableRow();
//set bgcolor on alternating rows
if (row % 2 == 1)
r.BgColor = "sky blue";
row++;
for (int i = 0; i < numc; i++)
{
HtmlTableCell c = new HtmlTableCell();
c.Controls.Add(new LiteralControl("row" + j.ToString() + ",col" +
i.ToString()));
r.Cells.Add(c);
}
Table1.Rows.Add(r);
}
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table id="Table1" cellpadding="3" cellspacing="0" border="1"
runat="server"/>
Table Rows:
<select id="selrow" runat="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br />
TAble Columns:
</div>
<table style="width:100%;">
<tr>
<td colspan="2">
Form No.:</td>
<td colspan="2">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
Name:</td>
<td colspan="2">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
Sex:</td>
<td colspan="2">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
Date Of Birth:
</td>
<td colspan="2">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
Class:</td>
<td colspan="2">
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="New" />
</td>
<td class="style1">
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Insert" />
</td>
<td class="style1">
<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
Text="Delete" />
</td>
<td style="text-align: center">
<asp:Button ID="Button4" runat="server" style="text-align: center"
Text="Show" onclick="Button4_Click" />
</td>
</tr>
<tr>
<td class="style1">
</td>
<td class="style1">
</td>
<td class="style1">
</td>
<td style="text-align: center">
</td>
</tr>
</table>
<br />
<br />
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
Connection.aspx.cs
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;
cn.Close();
}
catch
{
}
}
protected void Button4_Click(object sender, EventArgs e)
{
cn.Open();
OleDbDataReader dr;
s = "select * from emp";
cmd = new OleDbCommand(s, cn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
}
}
}
Output:
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<p style="margin-left: 100px">
<br />
</p>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Output:
8)Validation Control
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="validation.aspx.cs" Inherits="validation" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
text-align: center;
font-size: xx-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td class="style1" colspan="2">
<strong>Validation Control</strong></td>
</tr>
<tr>
<td>
Name:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Contact No.:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server"
ControlToValidate="TextBox2" ErrorMessage="Plz Enter Valid
No."></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Address:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"
Rows="5"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Date of Join:</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator2" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Plz Enter Valid
Date"
MaximumValue="05/31/2011"
MinimumValue="03/01/2011">Enter date of March, April or May
2011</asp:RangeValidator>
</td>
</tr>
<tr>
<td>
Email ID:</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"
Width="220px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="TextBox5" ErrorMessage="Valid Email
Address"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+
([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<br />
</div>
</form>
</body>
</html>
Output:
$("table").hide();
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1">
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>9</td><td>10</td><td>11</td><td>12</td></tr>
</table>
<br />
<input type="button" value="Hide the Table" />
</div>
</form>
</body>
</html>
Output: