Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

Index

S.No Questions Page No.


.
1. Write a C sharp program to generate prime numbers between 1 to 200 and also
print to the console. (ex. 1,2,3,5.....................199).
2. Write a program to print ARMSTRONG number.
3. Write a C sharp program using loop that examines all the numbers between 2
and 1000, and displays only Perfect numbers.
4. Write a C sharp program to accept an array of integers (10) and sort them in
ascending order.
5. Write a program to implement the concept of abstract class.
6. Write a program to implement the concept of sealed class.
7. Write a C sharp program for jagged array and display its item through foreach
loop.
8. Write a program to demonstrate boxing and unboxing.
9. Write a program to find number of digit, character, and punctuation in entered
string.
10. Write a program using C# for exception handling.
11. Write a program to implement multiple inheritances using interface.
12. Write a program in C# using a delegate to perform basic arithmetic operations
like addition, subtraction, division, and multiplication.
13. Demonstrate the concept of Multithreading using locks in C Sharp
14. Write a program to implement Indexer.
15. Write a program to design two interfaces that are having same name methods.
16. Write a program to implement method overloading.
17. Write a program to implement method overriding
18. Write a program in C sharp to create a calculator in windows form.
19. Create a front end interface in windows that enables a user to accept the details
of an employee like EmpId ,First Name, Last Name, Gender, Contact No,
Designation, Address and Pin. Create a database that stores all these details in a
table. Also, the front end must have a provision to Add, Update and Delete a
record of an employee.
20.
Create a database named MyDb (SQL or MS Access).Connect the database with
your window application to display the data in List boxes using Data Reader.
21.
Write a program using ADO.net to insert, update, delete data in back end
22.
Display the data from the table in a DataGridView control using dataset.
23.
Create a registration form in ASP.NET and use different types of validation
controls.
Practical Assignment

1. Write a C sharp program to generate prime numbers between 1


to 200 and also print to the console. (ex. 1,2,3,5.....................199).
using System;
class Program {
static void Main() {
bool isPrime = true;
int i, j;
Console.WriteLine("Prime Numbers are : ");
for (i = 2; i <= 200; i++) {
for (j = 2; j <= 200; j++) {
if (i != j && i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
Console.Write(" " + i);
}
isPrime = true;
}
Console.ReadKey();
}
}

2. Write a program to print ARMSTRONG number.


using System;
public class ArmstrongExample
{
public static void Main(string[] args)
{
int n,r,sum=0,temp;
Console.Write("Enter the Number = ");
n= int.Parse(Console.ReadLine());
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
Console.Write("Armstrong Number.");
else
Console.Write("Not Armstrong Number.");
}
}

3. Write a C sharp program using loop that examines all the numbers
between 2 and 1000, and displays only Perfect numbers.(A
perfect number is the one whose sum of their divisors equals the
number itself).For example given the number 6, the sum of its
divisors is 6(1+2+3).Hence, 6 is a perfect number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PerfectNumberInRange
{
class Program
{
static void Main(string[] args)
{
int startingNumber, endingNumbeer;
Console.WriteLine("Get All Perfect In Range of Between
two
Number");
Console.Write("Enter Starting Number : ");
startingNumber = int.Parse(Console.ReadLine());
Console.Write("Enter Ending Number : ");
endingNumbeer = int.Parse(Console.ReadLine());
Console.WriteLine("Below are the perfect number between
"+
startingNumber + " and " +
endingNumbeer);
for (int i = startingNumber; i <= endingNumbeer; i++)
{
decimal sum = 0;
for (int j = 1; j < i; j++)
{
if (i % j == 0)
sum = sum + j;
}
if (sum == i)
Console.WriteLine("\t" +i);
}
Console.ReadKey();
}
}
}

4. Write a C sharp program to accept an array of integers (10) and


sort them in ascending order.
using System;
public class Exercise11
{
public static void Main()
{
int[] arr1 = new int[10];
int n, i, j, tmp;
Console.Write("Sort elements of array in ascending order :\
n");
Console.Write("Input the size of array : ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Input {0} elements in the array :\n",n);
for(i=0;i<n;i++)
{
Console.Write("element - {0} : ",i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] < arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
Console.Write("Elements of array in sorted ascending order:\
n");
for(i=0; i<n; i++)
{
Console.Write("{0} ", arr1[i]);
}
Console.Write("\n\n");
}
}

5. Write a program to implement the concept of abstract class.


using System;
abstract class Animal
{
public abstract void animalSound();
public void sleep()
{
Console.WriteLine("Zzz");
}
}
class Pig : Animal
{
public override void animalSound()
{
Console.WriteLine("The pig says: wee wee");
}
}
class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
}
}

6. Write a program to implement the concept of sealed class.


using System;
sealed class SealedClass
{
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
SealedClass slc = new SealedClass();
int total = slc.Add(6, 4);
Console.WriteLine("Total = " + total.ToString());
}
}

7. Write a C sharp program for jagged array and display its item
through foreach loop.
using System;
public class Program
{
public static void Main(string[] args)
{
string[][] str = new string[5][];
str[0] = new string[5];
str[1] = new string[10];
str[2] = new string[20];
str[3] = new string[50];
str[4] = new string[10];
str[0][0] = "Pune";
str[1][0] = "Kolkata";
str[2][0] = "Bangalore";
str[3][0] = "The pink city named Jaipur";
str[4][0] = "Hyderabad";
foreach(string[] i in str)
foreach(string s in i)
if(!string.IsNullOrEmpty(s))
Console.WriteLine(s);
Console.ReadKey();
}
}

8. Write a program to demonstrate boxing and unboxing.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practical
{
class Test
{
static void Main()
{
int i = 1;
object o = i;
int j = (int)o;
Console.WriteLine("value of o = "+o);
Console.WriteLine("value of j = "+j);
Console.ReadLine();

}
}
}

9. Write a program to find number of digit, character, and


punctuation in entered string.
using System;
public class Exercise7
{
public static void Main()
{
string str;
int alp, digit, splch, i,l;
alp = digit = splch = i = 0;
Console.Write("Input the string : ");
str = Console.ReadLine();
l=str.Length;
while(i<l)
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alp++;
}
else if(str[i]>='0' && str[i]<='9')
{
digit++;
}
else
{
splch++;
}
i++;
}
Console.Write("Number of Alphabets in the string is : {0}\n",
alp);
Console.Write("Number of Digits in the string is : {0}\n",
digit);
Console.Write("Number of Special characters in the string is :
{0}\n\n", splch);
}
}

10.Write a program using C# for exception handling.


using System;
namespace ErrorHandlingApplication
{
class DivNumbers
{
int result;
DivNumbers()
{
result = 0;
}
public void division(int num1, int num2)
{
try
{
result = num1 / num2;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception caught: {0}", e);
}
finally
{
Console.WriteLine("Result: {0}", result);
}
}
static void Main(string[] args)
{
DivNumbers d = new DivNumbers();
d.division(25, 0);
Console.ReadKey();
}
}
}

11.Write a program to implement multiple inheritances using
interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MultipleInheritApplication
{
interface calc1
{
int add(int a, int b);
}
interface calc2
{
int sub(int x, int y);
}
interface calc3
{
int mul(int r, int s);
}
interface calc4
{
int div(int c, int d);
}
class Calculation : calc1, calc2, calc3, calc4
{
public int result1;
public int add(int a, int b)
{
return result1 = a + b;
}
public int result2;
public int sub(int x, int y)
{
return result2 = x - y;
}
public int result3;
public int mul(int r, int s)
{
return result3 = r * s;
}
public int result4;
public int div(int c, int d)
{
return result4 = c / d;
}
class Program
{
static void Main(string[] args)
{
Calculation c = new Calculation();
c.add(8, 2);
c.sub(20, 10);
c.mul(5, 2);
c.div(20, 10);
Console.WriteLine("Multiple Inheritance concept Using
Interfaces : ");
Console.WriteLine("Addition: " + c.result1);
Console.WriteLine("Substraction: " + c.result2);
Console.WriteLine("Multiplication :" + c.result3);
Console.WriteLine("Division: " + c.result4);
Console.ReadKey();
}
}
}
}

12.Write a program in C# using a delegate to perform basic


arithmetic operations like addition, subtraction, division, and
multiplication.
using System;
delegate int NumberChanger(int n);
namespace example
{
class Delegate
{
static int num = 10;
public static int AddNum(int a)
{
num += a;
return num;
}
public static int MultNum(int b)
{
num *= b;
return num;
}
public static int getNum()
{
return num;
}
static void Main(string[] args)
{
NumberChanger n1 = new NumberChanger(AddNum);
NumberChanger n2 = new NumberChanger(MultNum);
n1(25);
Console.WriteLine("Value of Num: {0}", getNum());
n2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}

13.Demonstrate the concept of Multithreading using locks in C Sharp.
using System;
using System.Threading;
using System.Diagnostics;
namespace Threadlocking
{
class Department
{
private Object thisLock = new Object();
int salary;
Random r = new Random();
public Department(int initial)
{
salary = initial;
}
int Withdraw(int amount)
{
if (salary < 0)
{
throw new Exception("Negative Balance");
}
lock (thisLock)
{
if (salary >= amount)
{
Console.WriteLine("salary before Withdrawal : " +
salary);
Console.WriteLine("Amount to Withdraw : -" +
amount);
salary = salary - amount;
Console.WriteLine("salary after Withdrawal : " +
salary);
return amount;
}
else
{
return 0;
}
}
}
public void DoTransactions()
{
for (int i = 0; i < 100; i++)
{
Withdraw(r.Next(1, 100));
}
}
}
class Process
{
static void Main()
{
Thread[] threads = new Thread[10];
Department dep = new Department(100);
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new
ThreadStart(dep.DoTransactions));
threads[i] = t;
}
for (int i = 0; i < 10; i++)
{
threads[i].Start();
}
Console.Read();
}
}
}

14.Write a program to implement Indexer.


using System;
namespace Indexer_example1
{
class Program
{
class IndexerClass
{
private string[] names = new string[5];
public string this[int i]
{
get
{
return names[i];
}
set
{
names[i] = value;
}
}
}
static void Main(string[] args)
{
IndexerClass Team = new IndexerClass();
Team[0] = "Rocky";
Team[1] = "Teena";
Team[2] = "Ana";
Team[3] = "Victoria";
Team[4] = "Yani";
for (int i = 0; i < 5; i++)
{
Console.WriteLine(Team[i]);
}
Console.ReadKey();
}
}
}

15.Write a program to design two interfaces that are having same


name methods.
using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
public void Hello()
{
Console.WriteLine("Hello to all");
}
}
public class interfacetest
{
public static void Main()
{
Test Obj1 = new Test();
Obj1.Hello();
}
}

16.Write a program to implement method overloading.


using System;
public class Cal{
public static int add(int a,int b){
return a + b;
}
public static int add(int a, int b, int c)
{
return a + b + c;
}
public class TestMemberOverloading
{
public static void Main()
{
Console.WriteLine(Cal.add(12, 23));
Console.WriteLine(Cal.add(12, 23, 25));
}
}

17.Write a program to implement method overriding.


using System;
public class Animal
{
public virtual void eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog: Animal
{
public override void eat()
{
Console.WriteLine("Eating bread...");
}
}
public class TestOverriding
{
public static void Main()
{
Dog d = new Dog();
d.eat();
}
}

18.Write a program in C sharp to create a calculator in windows


form.
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 Calculator1
{
public partial class Form1 : Form
{
Double value = 0;
String op = " ";
boolisop = false;
public Form1()
{
InitializeComponent();
}
private void button_click(object sender, EventArgs e)
{
if (result.Text == "0" || isop)
{
result.Clear();
}
Button button = (Button)sender;
result.Text += button.Text;
isop = false;
}
private void operator_click(object sender, EventArgs e)
{
isop = true;
Button button = (Button)sender;
op = button.Text;
value = Double.Parse(result.Text);
}
private void button15_Click(object sender, EventArgs e)
{
result.Text= "0";
}
private void button16_Click(object sender, EventArgs e)
{
switch (op)
{
case "+":
result.Text=(value +
Double.Parse(result.Text)).ToString();
break;
case "-":
result.Text = (value -
Double.Parse(result.Text)).ToString();
break;
case "*":
result.Text = (value *
Double.Parse(result.Text)).ToString();
break;
case "/":
result.Text=( value /
Double.Parse(result.Text)).ToString();
break;
default:
break;
}
}
}
}

19.Create a front end interface in windows that enables a user to


accept the details of an employee like EmpId ,First Name, Last
Name, Gender, Contact No, Designation, Address and Pin. Create
a database that stores all these details in a table. Also, the front
end must have a provision to Add, Update and Delete a record of
an employee.
Create table Employee(Empid int ,firstname varchar(15),lastname
varchar(15),designation varchar(15), Empaddress
varchar(15),contact int, pin int,gender varchar(5));

20.Create a database named MyDb (SQL or MS Access).Connect the
database with your window application to display the data in List
boxes using Data Reader.
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;
using System.Data.SqlClient;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
SqlConnection c = new SqlConnection ("server=(localdb)\\
MSSQLLocalDB;integrated
security=True;database=Master");
SqlCommandcmd = new SqlCommand("select * from
Mydb", c);
SqlDataReaderdr;
c.Open();
dr = cmd.ExecuteReader();
while(dr.Read())
{
listBox1.Items.Add(dr.GetInt32(0));
listBox2.Items.Add(dr.GetString(1));
listBox3.Items.Add(dr.GetString(2));
listBox4.Items.Add(dr.GetInt32(3));
}
c.Close();
}
}
}


21.Write a program using ADO.net to insert, update, delete data in
back end.
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;
using System.Data.SqlClient;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection c = new SqlConnection("server=(localdb)\\
MSSQLLocalDB;integrated security=True;database=Master");
private void button1_Click(object sender, EventArgs e)
{
SqlCommandcmd = new SqlCommand("insert into Mydb
values(@id,@name,@course,@age)",c);
cmd.Parameters.AddWithValue("@id",textBox1.Text);
cmd.Parameters.AddWithValue("@name", textBox2.Text);
cmd.Parameters.AddWithValue("@course", textBox3.Text);
cmd.Parameters.AddWithValue("@age",textBox4.Text);
c.Open();
cmd.ExecuteNonQuery();
c.Close();
MessageBox.Show("inserted successfully!!");
}
private void button3_Click(object sender, EventArgs e)
{
SqlCommandcmd = new SqlCommand("delete from Mydb
where id='"+textBox1.Text+"'",c);
c.Open();
cmd.ExecuteNonQuery();
c.Close();
MessageBox.Show("record deleted successfully!!");
}
private void button2_Click(object sender, EventArgs e)
{
SqlCommandcmd = new SqlCommand("update Mydb set
name=@name,course=@course,age=@age where id=@id", c);
cmd.Parameters.AddWithValue("@id", textBox1.Text);
cmd.Parameters.AddWithValue("@name",textBox2.Text);
cmd.Parameters.AddWithValue("@course", textBox3.Text);
cmd.Parameters.AddWithValue("@age", textBox4.Text);
c.Open();
cmd.ExecuteNonQuery();
c.Close();
MessageBox.Show("record updated successfully!!");
}
}
}

22.Display the data from the table in a DataGridView control using


dataset.
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;
using System.Data.SqlClient;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new
SqlConnection("server=(localdb)\\MSSQLLocalDB;integrated
security=true;database=master");
SqlDataAdapter da = new SqlDataAdapter("select *from
t3",con);
DataSet ds = new DataSet();
da.Fill(ds, "t3");
dataGridView1.DataSource = ds.Tables[0];
}
}
}

23.Create a registration form in ASP.NET and use different types of


validation controls.
 <%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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 {
width: 122px;
}
.style2 {
width: 239px;
}
.style3 {
text-align: left;
text-decoration: underline;
font-family: Arial, Helvetica, sans-serif;
font-size: large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1"
runat="server"></asp:ToolkitScriptManager>
<div>
<table style="width:100%;">
<caption class="style3">
<strong>Registration Form with
Validation</strong>
</caption>
<tr>
<td class="style1">
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label1" runat="server"
Text="FirstName:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox1" runat="server"
Height="22px" MaxLength="20" Width="158px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator6" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Please Enter your
First Name" ForeColor="#CC0000"></asp:RequiredFieldValidator>
</td>
<td>
<asp:FilteredTextBoxExtender
ID="FilteredTextBoxExtender1" TargetControlID="TextBox1"
FilterType="LowercaseLetters,UppercaseLetters,Custom"
runat="server"></asp:FilteredTextBoxExtender>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label2" runat="server"
Text="LastName:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox2" runat="server"
Height="22px" MaxLength="10" Width="158px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Please Enter your
Last Name" ForeColor="#CC0000"></asp:RequiredFieldValidator>
</td>
<td>
<asp:FilteredTextBoxExtender
ID="FilteredTextBoxExtender2" TargetControlID="TextBox2"
FilterType="LowercaseLetters,UppercaseLetters,Custom"
runat="server"></asp:FilteredTextBoxExtender>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label3" runat="server"
Text="Email:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox3" runat="server"
Height="22px" MaxLength="15" Width="158px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Please Enter your
Email ID" ForeColor="#CC0000"></asp:RequiredFieldValidator>
</td>
<td>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox3" ValidationExpression="\w+([-+.']\
w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Invalid Email
Id"></asp:RegularExpressionValidator>
<td>
</td>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label4" runat="server"
Text="Phone No. :"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox4" runat="server"
Height="22px" MaxLength="10" Width="158px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Please Enter your
Phone No" ForeColor="#CC0000"></asp:RequiredFieldValidator>
</td>
<td>
<asp:FilteredTextBoxExtender
ID="FilteredTextBoxExtender3" TargetControlID="TextBox4"
FilterType="Numbers"
runat="server"></asp:FilteredTextBoxExtender>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label5" runat="server"
Text="Location:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox5" runat="server"
Height="22px" MaxLength="15" Width="158px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator5" runat="server"
ControlToValidate="TextBox5" ErrorMessage="Please Enter your
Location" ForeColor="#CC0000"></asp:RequiredFieldValidator>
</td>
<td>
<asp:FilteredTextBoxExtender
ID="FilteredTextBoxExtender4" TargetControlID="TextBox5"
FilterType="Numbers,LowercaseLetters,UppercaseLetters"
runat="server"></asp:FilteredTextBoxExtender>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td class="style2">
<asp:Button ID="Button1" runat="server"
onclick="Button1_Click" Text="Submit" />
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

You might also like