Professional Documents
Culture Documents
Netpractical File (1) PDF
Netpractical File (1) PDF
NAME SATYAM
SINGH
ENROLLMENT NO. R190529044
SUBJECT C# with .Net Prog. (DSE
2.1)
1
INDEX
2
13 Write a program in C# using a delegate to 16
perform basic arithmetic operations like
addition, subtraction, division, and
multiplication.
14 Write a program to get the user’s name from the 17
console and print it using different namespace.
3
C# Programs
Ques 1: WAP TO generate prime numbers from 1 to 1000 using
System;
namespace Program1_PrimeNumber
{
classProgram
{
staticvoid Main(string[] args)
{
int range = 1000;
int flag = 0;
Console.WriteLine("Prime Number :");
for (inti = 1; i<= range; i++) { flag
= 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
flag++;
}
}
if (flag == 2) {
Console.Write(" ," + i);
}
}
Console.ReadKey();
}
}
}
Output :
4
}
if (t == temp)
{
Console.WriteLine("Given number is armstrong.");
}
else {
Console.WriteLine("Given number is not armstrong.");
}
Console.ReadKey();
}
}
}
Output :
Ques 3 : WAP using loop that examines all the numbers between 2 & 1000, and display only
perfect number.
using System;
namespace Program3_PerfectNumber
{
classProgram
{
staticvoid Main(string[] args)
{
int start = 1, range = 10000;
int n = 0;
for (inti = start; i<= range; i++) {
n = 0;
for (int j = 1 ; j<=(i/2); j++)
{
if (i % j == 0) {
n = n + j;
} }
if (n == i)
{
Console.WriteLine(i);
}
}
Console.ReadKey();
}
}
}
5
Ques4 : WAP to accept an array of integer and sort them in ascending order.
using System;
namespace Program4_SortArray
{
classProgram
{
staticvoid Main(string[] args)
{
constint range = 10;
int[] arr = newint[range] { 23, 45, 67, 8, -9, 76, 88, 12, 7, 2 };
Console.WriteLine("\nArray : ");
Sort.show(arr, range); for (inti = 0; i<
range-1; i++)
{
for (int j = 0; j < (range - i - 1); j++)
{
if (arr[j] >arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
Console.WriteLine("\nSorted Array : ");
Sort.show(arr, range);
Console.ReadKey();
}
}
classSort {
publicstaticvoid show(int[] arr,int range) {
for (inti = 0; i< range; i++) {
Console.Write(arr[i] + " ");
}
}
}
}
classProgram: University
6
{
publicoverridevoidUniversityName()
{
Console.WriteLine("SGRR-UNIVERSITY");
}
staticvoid Main(string[] args)
{
UniversityUv = newProgram(); // reference variable of abstract class
Uv.UniversityName();
Uv.CourseName();
Console.ReadKey();
}
}
}
7
Ques 7 : Write a C# Program for jagged array and displayed items through foreach loop
using System; class
JaggedArray {
static void Main() {
int size = 5;
Ques 8 : Write a program in C Sharp using a class that gets the information about employee's such
as Emp Id, First Name, Last Name, Basic Salary, Grade, Address, Pin Code and Contact Number.
Write a method that calculates the Gross Salary (Basic +DA+HRA) and returns to the calling
program and another method for the Net salary (Gross - (P.F + Income Tax)).Finally write a
method that prints, a pay slip of an employee, containg all the above components in a proper format
to the console.(Grade A = 20,000 , B=15,000 and C=10,000) DA=56% and HRA=20%., Pf=780,
ITax.
using System; class SalaryCalculation { string
8
ITAX=0; float DA=0.0f, HRA=0.0f; public
void getData() {
id = Console.ReadLine();
fname = Console.ReadLine();
lname =Console.ReadLine();
char.Parse(Console.ReadLine());
address = Console.ReadLine();
pincode = Int32.Parse(Console.ReadLine());
contactNumber = Console.ReadLine();
setBasicSalary(grade);
{ if(grade == 'A')
= 15000;
else
basicSalary = 10000;
9
public void showData() {
float s = basicSalary+DA+HRA;
return s;
return s;
class HelloWorld {
netSalary = S1.calculateNetSalary(grossSalary);
10
}
11
Ques 9 : Write a program to demonstrate boxing and unboxing.
using System; class BoxingUnboxing { static void Main() { intval = 5;
12
Ques 10 : Write a program to find number of digit, character, and punctuation in entered
digit++;
characters++;
}else {
punctations++;
13
Ques 11 : Write a program using C# for exception handling.
using System; class
ExceptionHandling {
num1 = Int32.Parse(System.Console.ReadLine());
num2 = Int32.Parse(System.Console.ReadLine());
try {
System.Console.WriteLine(res);
catch(Exception e) {
System.Console.WriteLine(e.Message);
finally {
14
Ques 12 : Write a program to implement multiple inheritances using interface.
using System;
namespace MULTIPLE_INHERITENCE
{
interface Father {
void give_name();
void face_colour();
}
interface Mother {
void give_name();
void voice();
}
public class Child : Father, Mother {
public void give_name() {
Console.WriteLine("THE NAME OF THE CHILD WILL BE VIBHOR");
}
public void face_colour() {
Console.WriteLine("fair");
}
public void voice() {
Console.WriteLine("Sweet");
}
}
class Program {
static void Main(string[] args)
{ Child ch = new Child();
ch.give_name();
ch.face_colour(); ch.voice();
}
}
}
Ques 13 : Write a program in C# using a delegate to perform basic arithmetic operations like
addition, subtraction, division, and multiplication.
15
using System;
public delegate void Calculate(float x, float y);
class Calc {
public void addition(float x, float y){
Console.WriteLine("Output - " + (x+y) );
}
public void subtraction(float x, float y){
Console.WriteLine("Output - " + (x-y) );
}
public void multiplication(float x, float y){
Console.WriteLine("Output - " + (x*y) );
}
public void division(float x, float y){
Console.WriteLine("Output - " + (x/y) );
}
}
class HelloWorld {
static void Main() {
int x, y;
Calc c = new Calc();
Console.WriteLine("Enter two numbers : ");
x = Int32.Parse(Console.ReadLine()); y=
Int32.Parse(Console.ReadLine());
Console.WriteLine("\n1.Add\n2.Subtract\n3.Multiply\n4.Division\n5.Exit\nEnter your choice :
"); intch = Int32.Parse(System.Console.ReadLine()); switch(ch) { case 1:{
Calculate c1 = new
Calculate(c.addition); c1(x,y);
break;
}
case 2:{
Calculate c1 = new Calculate(c.subtraction);
c1(x,y); break;
}
case 3:{
Calculate c1 = new Calculate(c.multiplication);
c1(x,y);
break;
16
}
case 4:{
Calculate c1 = new
Calculate(c.division); c1(x,y);
break;
}
default: {
Console.WriteLine("Wrong Choice!");
break;
}
}
}
}
Ques 14 : Write a program to get the user’s name from the console and print it using different
namespace.
using System; namespace
= System.Console.ReadLine();
}
class NamespaceConcept {
17
obj.getInput();
18
Ques 16 : Write a program to implement Indexer
using System;
class HelloWorld {
this[int index] {
I1[0] = "Sushil";
I1[1] = "Ajay";
I1[2] = "Rameshwar";
I1[3] = "Anjali";
Console.WriteLine("Names :- \n");
Console.WriteLine(I1[0]);
Console.WriteLine(I1[1]);
Console.WriteLine(I1[2]);
Console.WriteLine(I1[3]);
}
}
19
Ques 17 : Write a program to design two interfaces that are having same name methods how we
can access these methods in another class.
using System; namespace
Hospital { interface
Demo1 {
void role();
interface Demo2 {
void role();
void Demo1.role() {
Console.WriteLine("Role : Doctor");
void Demo2.role() {
Console.WriteLine("Role : Chemist");
class InterfaceAmbiguityConcept {
D1.role();
D2.role();
RunTimePolymorphism {
20
class BaseClass { public void
return num1+num2+num3;
class MethodOverloading {
obj.add(2,3); obj.add(55,5,31);
class BaseClass {
21
}
class MethodOverriding {
obj.fun();
22
23
{
SqlConnection con = newSqlConnection("server = ADMIN-PC\\SQLEXPRESS; Integrated Security = true; database
= master");
SqlCommandcmd = newSqlCommand("select * from data", con); con.Open();
SqlDataReaderdr = cmd.ExecuteReader(); while
(dr.Read()) {
IDContainer.Items.Add(dr.GetString(0));
NameContainer.Items.Add(dr.GetString(1));
AddressContainer.Items.Add(dr.GetString(2));
}
Ques 22 : Write a program using ADO.net to insert, update, delete data in back end
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace ADO.NET
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid button4_Click(object sender, EventArgs e)
24
{
SqlConnection con = newSqlConnection("server = ADMIN-PC\\SQLEXPRESS; Integrated Security = true; database
= master");
SqlCommandcmd = newSqlCommand("select * from data", con); con.Open();
SqlDataReaderdr = cmd.ExecuteReader();
while (dr.Read()) {
IDContainer.Items.Add(dr.GetString(0));
NameContainer.Items.Add(dr.GetString(1));
AddressContainer.Items.Add(dr.GetString(2));
}
con.Close();
}
25
Ques 23 : Display the data from the table in a DataGridView control using dataset.
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceDisconnectedArch
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
26
SqlConnection con = newSqlConnection("server = ADMIN-PC\\SQLEXPRESS; Integrated Security = true; database
= master");
SqlDataAdapter da = newSqlDataAdapter("select * from data",con);
DataSet ds = newDataSet();
da.Fill(ds, "data");
dataGridView1.DataSource = ds.Tables[0];
}
}
}
End …
26