Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C# Practicals: Practical 1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

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 System.Collections.Generic;
using System.Text;

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;
Console.Clear();
Console.WriteLine("Enter any number");
number = Convert.ToInt32(Console.ReadLine());
do
{
Console.WriteLine("1.Sum of digits");
Console.WriteLine("2.Reversal");
Console.WriteLine("3.Pallindrome");
Console.WriteLine("4.Exit");
Console.WriteLine("Enter choice");
ch = Convert.ToInt32(Console.ReadLine());
switch (ch)
{
case 1:
Console.WriteLine(number);
sumofdig = calculate_sum(number);
Console.WriteLine("Sum of digits:" + sumofdig);
break;
case 2:
rev = reversal(number);
Console.WriteLine("Reversed Number:" + rev);
break;
case 3:
pal = reversal(number);
Console.WriteLine((number == pal) ? "Pallindrome" : "Not a Pallindrome");
break;
case 4:
Console.WriteLine("Exiting...");
break;
} Console.ReadKey();
} 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.

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

private void btndisplay_Click(object sender, EventArgs e)


{
String msg;
msg = "Mailing label:" + Environment.NewLine + Environment.NewLine;
msg = msg + "Name :" + txtname.Text + Environment.NewLine;
msg = msg + "Address :" + txtaddress.Text + Environment.NewLine;
txtdisplay.Text = 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.

Form1.cs
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.Odbc;

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);

objConnection.Open();

String strSQL = "";


strSQL = "insert into Student values (" + txtstudid.Text + ",";
strSQL = strSQL + txtm1.Text + ",";
strSQL = strSQL + txtm2.Text + "," + txtm3.Text + ")";

OdbcCommand objCommand1 = null;


objCommand1 = new OdbcCommand(strSQL, objConnection);
int count = objCommand1.ExecuteNonQuery();
MessageBox.Show(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


myAdapter.Fill(myData, "Student");

dgMarks.DataSource=myData.Tables["Student"];

objCommand2.ExecuteReader();

}
}
}
OUTPUT:

You might also like