C# PRACTICALS
PRACTICAL 1
Write a menu driven console based application in C# to enter any number and depending on the
choice(1-Sum of digits,2-reverse the digits and 3- palindrome) display the result.
using System;
using [Link];
using [Link];
namespace sum_rev_palin
{
class Program
{
public static int calculate_sum(int n)
{
int temp, sum = 0;
do
{
temp = n % 10;
n /= 10;
sum += temp;
} while (n != 0);
return (sum);
}
public static int reversal(int number)
{
int[] a;
a = new int[10];
int rev;
int i = 0, j = 0;
string s = "";
do
{
a[i++] = number % 10;
number = number / 10;
} while (number != 0);
for (j = 0; j < i; j++)
{
s = s + a[j];
}
rev = Convert.ToInt32(s);
return (rev);
}
static void Main(string[] args)
{
int number = 0, ch, sumofdig, rev, pal;
[Link]();
[Link]("Enter any number");
number = Convert.ToInt32([Link]());
do
{
[Link]("[Link] of digits");
[Link]("[Link]");
[Link]("[Link]");
[Link]("[Link]");
[Link]("Enter choice");
ch = Convert.ToInt32([Link]());
switch (ch)
{
case 1:
[Link](number);
sumofdig = calculate_sum(number);
[Link]("Sum of digits:" + sumofdig);
break;
case 2:
rev = reversal(number);
[Link]("Reversed Number:" + rev);
break;
case 3:
pal = reversal(number);
[Link]((number == pal) ? "Pallindrome" : "Not a Pallindrome");
break;
case 4:
[Link]("Exiting...");
break;
} [Link]();
} while (ch != 4);
}
}
}
OUTPUT:
PRACTICAL 2
Write a program in C# to create a window based application as follows:
On click of display, the name and address should be displayed in the textbox below.
On click of exit, the program should come out of runtime.
[Link]
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Mail_application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btndisplay_Click(object sender, EventArgs e)
{
String msg;
msg = "Mailing label:" + [Link] + [Link];
msg = msg + "Name :" + [Link] + [Link];
msg = msg + "Address :" + [Link] + [Link];
[Link] = msg;
}
private void btnexit_Click(object sender, EventArgs e)
{
Close();
}
}
}
OUTPUT:
PRACTICAL 4
Write a database program in C# to insert the data from the controls to the marks table. As soon
as the user inserts a new record the data should appear in the datagrid on the same form.
Use OdbcConnection, OdbcDataAdapter and DataSet to populate the data from marks table to
the gridview.
[Link]
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btninsert_Click(object sender, EventArgs e)
{
string accessconn = "";
accessconn = "DSN=Mydata";
OdbcConnection objConnection = null;
objConnection = new OdbcConnection(accessconn);
[Link]();
String strSQL = "";
strSQL = "insert into Student values (" + [Link] + ",";
strSQL = strSQL + [Link] + ",";
strSQL = strSQL + [Link] + "," + [Link] + ")";
OdbcCommand objCommand1 = null;
objCommand1 = new OdbcCommand(strSQL, objConnection);
int count = [Link]();
[Link](count + " rows inserted");
string selStatement = "select * from Student";
OdbcCommand objCommand2 = null;
objCommand2 = new OdbcCommand(selStatement, objConnection);
OdbcDataAdapter myAdapter = new OdbcDataAdapter(objCommand2);
//' instantiate dataset object
DataSet myData = new DataSet();
// fill with query results
[Link](myData, "Student");
[Link]=[Link]["Student"];
[Link]();
}
}
}
OUTPUT: