Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

BATCH: 2019-2022

BACHELOR OF COMPUTER APPLICATIONS(BCA).


V - SEMESTER

External Practical File

NAME SATYAM
SINGH
ENROLLMENT NO. R190529044
SUBJECT C# with .Net Prog. (DSE
2.1)

SUBMITTED BY: SUBMITTED


TO:

1
INDEX

S.NO. PROGRAM PAGE


NO.
1 WAP TO generate prime numbers from 1 to 4
1000
2 WAP to print ARMSTRONG number. 4
3 WAP using loop that examines all the numbers 5
between 2 & 1000, and display only perfect
number.
4 WAP to accept an array of integer and sort them 6
in ascending order.
5 WAP to implement the concept of abstract 6
class.
6 Write a program to implement the concept of 7
sealed class.
7 Write a C# Program for jagged array and 8
displayed items through foreach loop
8 Write a program in C Sharp using a class that 9
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, containing 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.
9 Write a program to demonstrate boxing and 12
unboxing
10 Write a program to find number of digit, 13
character, and punctuation in entered string.
11 Write a program using C# for exception 14
handling.
12 Write a program to implement multiple 15
inheritances using interface.

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.

15 Program to Implement Bubble Sort. 18


16 Write a program to implement Indexer 19
17 Write a program to design two interfaces that 20
are having same name methods how we can
access these methods in another class.
18 Write a program to implement method 21
overloading.
19 Write a program to implement method 22
overriding.
20 Write a program in C sharp to create a 23
calculator in windows form.
21 Create a database named MyDb (SQL or MS 24
Access). Connect the database with your
window application to display the data in List
boxes using Data Reader.
22 Write a program using ADO.net to insert, 25
update, delete data in back end
23 Display the data from the table in a 27
DataGridView control using dataset.

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 :

Ques 2: WAP to print ARMSTRONG number.


using System;
namespace Program2_ArmstrongNumber
{
classProgram
{
staticvoid Main(string[] args)
{
intnum, temp=0;
int digit = 0;
Console.WriteLine("Enter a number : ");
num = Int32.Parse(Console.ReadLine());
int t = num; while (num> 0) {
digit = num % 10;
temp = temp + (digit * digit * digit);
num /= 10;

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] + " ");
}
}
}
}

Ques 5 : WAP to implement the concept of abstract class.


using System; namespace
Program5_AbstractClass
{
publicabstractclassUniversity { publicabstractvoidUniversityName();
// non-implemented method
publicvoidCourseName() { // implemented method
Console.WriteLine("BCA");
}
}

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

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


using System;
namespace Program6_SealedClass
{
publicsealedclassUniversity// we cannot inherit this class.
{
publicvoidUniversityName()
{
Console.WriteLine("SGRR-UNIVERSITY");
}
publicvoidCourseName()
{
Console.WriteLine("BCA");
}
}
classProgram
{
staticvoid Main(string[] args)
{
UniversityUv = newUniversity();
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;

int[][] arr = new int[size][]; arr[0] = new


int[] {1,2,3,4,5}; arr[1] = new int[]
{11,12,14,15}; arr[2] = new int[]
{01,2322,123,-24,43,2,25}; arr[3] = new
int[] {31,32,33,324,35}; arr[4] = new int[]
{41,42};

for(int j=0; j<size; j++) {


System.Console.Write("Array " + (j+1) + ": "); foreach
(inti in arr[j]) {
System.Console.Write(i + " ");
}
System.Console.WriteLine();
}
}
}

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

id, fname,lname,contactNumber,address; char

grade; intbasicSalary,pincode, PF=780,

8
ITAX=0; float DA=0.0f, HRA=0.0f; public

void getData() {

Console.WriteLine("Enter your Emp Id : ");

id = Console.ReadLine();

Console.WriteLine("Enter your first name : ");

fname = Console.ReadLine();

Console.WriteLine("Enter your last name : ");

lname =Console.ReadLine();

Console.WriteLine("Enter your grade : "); grade =

char.Parse(Console.ReadLine());

Console.WriteLine("Enter your address : ");

address = Console.ReadLine();

Console.WriteLine("Enter your pincode : ");

pincode = Int32.Parse(Console.ReadLine());

Console.WriteLine("Enter your contact number : ");

contactNumber = Console.ReadLine();

setBasicSalary(grade);

void setBasicSalary(char grade)

{ if(grade == 'A')

basicSalary = 20000; else

if(grade == 'B') basicSalary

= 15000;

else

basicSalary = 10000;

DA = ((basicSalary * 56) / 100);

HRA = (float)((basicSalary * 20) / 100);

9
public void showData() {

Console.WriteLine("\nEmployee Details . . .\n"); Console.WriteLine("EmpID :" + id);

Console.WriteLine("Name :"+fname+" "+lname);

Console.WriteLine("Grade : " + grade);

Console.WriteLine("Contact Number : " + contactNumber);

Console.WriteLine("Address : " + address);

Console.WriteLine("Pincode : " + pincode);

Console.WriteLine("Basic Salary : "+ basicSalary);

Console.WriteLine("DA : "+ DA);

Console.WriteLine("HRA : "+ HRA);

public float calculateGrossSalary() {

float s = basicSalary+DA+HRA;

return s;

public float calculateNetSalary(float grossSalary)

{ float s = (grossSalary - (PF + ITAX));

return s;

class HelloWorld {

static void Main() {

SalaryCalculation S1 = new SalaryCalculation();

S1.getData(); S1.showData(); float

grossSalary = S1.calculateGrossSalary(); float

netSalary = S1.calculateNetSalary(grossSalary);

Console.WriteLine("\n\nGross Salary : "+ grossSalary);

Console.WriteLine("\n\nNet Salary : "+ netSalary); Console.ReadKey();

10
}

11
Ques 9 : Write a program to demonstrate boxing and unboxing.
using System; class BoxingUnboxing { static void Main() { intval = 5;

object o1 = val; // Boxing - Converting an value type variable to reference type.

System.Console.WriteLine("Value of object type variable o1 : " + o1);

intnew_val = (int)o1; // Unboxing - Converting an reference type

// variable to value type.

System.Console.WriteLine("Value of int type variable o1 : " + new_val);


}

12
Ques 10 : Write a program to find number of digit, character, and punctuation in entered

string. using System; class Counter { static void Main() {

System.Console.Write("Enter an string : ");

string str = System.Console.ReadLine();

System.Console.WriteLine("\nString is : " + str);

int digit = 0, characters = 0, punctations = 0;

for (inti=0; i<str.Length; i++) { char ch =

str[i]; if(ch>='0' &&ch<= '9') {

digit++;

}else if ((ch>= 'A' &&ch<='Z') || (ch>= 'a' &&ch<='z')) {

characters++;

}else {

punctations++;

System.Console.WriteLine("No. Of digits - " + digit);

System.Console.WriteLine("No. Of characters - " + characters);

System.Console.WriteLine("No. Of punctations - " + punctations);

13
Ques 11 : Write a program using C# for exception handling.
using System; class

ExceptionHandling {

static void Main() {

int num1 , num2;

System.Console.WriteLine("Enter two numbers : ");

num1 = Int32.Parse(System.Console.ReadLine());

num2 = Int32.Parse(System.Console.ReadLine());

try {

float res = num1/num2;

System.Console.WriteLine(res);

catch(Exception e) {

System.Console.WriteLine(e.Message);

finally {

System.Console.WriteLine("Program Terminated SuccessFully");

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

UserInput { class Demo {

public string username;

public void getInput() {

System.Console.Write("Enter username : "); username

= System.Console.ReadLine();

}
class NamespaceConcept {

static void Main(string[] args) {

UserInput.Demoobj = new UserInput.Demo();

17
obj.getInput();

System.Console.WriteLine("Username Value is : " + obj.username);

Ques 15 : Program to Implement Bubble Sort.


using System; namespace
BubbleSort {
class Program {
static void Main(string[] args) {
Console.WriteLine("Enter the value of n : ");
int n = Int32.Parse(Console.ReadLine());
int[] a = new int[n];
Console.WriteLine("Enter " + n + " Elements : ");
for (inti = 0; i< n; i++) {
a[i] = Int32.Parse(Console.ReadLine());
}
// Bubble Sort for (inti
= 0; i< n; i++) { for (int j =
0; j < n - i - 1; j++) { if
(a[j] > a[j+1]) { int temp =
a[j]; a[j] = a[j+1];
a[j+1] = temp;
}
}
}
Console.WriteLine("Sorted array : ");
for (inti = 0; i< n; i++){
Console.Write(a[i] + ", ");
}
Console.ReadLine();
}
}
}

18
Ques 16 : Write a program to implement Indexer
using System;

class HelloWorld {

class Indexer { string[] names

= new string[10]; public string

this[int index] {

get { return names[index]; }

set { names[index] = value; }

static void Main() {

Indexer I1 = new Indexer();

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

class Dummy : Demo1, Demo2 {

void Demo1.role() {

Console.WriteLine("Role : Doctor");

void Demo2.role() {

Console.WriteLine("Role : Chemist");

class InterfaceAmbiguityConcept {

static void Main() {

Demo1 D1 = new Dummy();

D1.role();

Demo2 D2 = new Dummy();

D2.role();

Ques 18 : Write a program to implement method overloading.


using System; namespace

RunTimePolymorphism {

20
class BaseClass { public void

add(int num1, int num2) {

System.Console.WriteLine("Addition is : " + (num1+num2));

public void add(int num1,int num2, int num3) {

System.Console.WriteLine("Addition is : " + (num1+num2+num3));

public float add(int num1,float num2, int num3) {

return num1+num2+num3;

class MethodOverloading {

static void Main() {

BaseClassobj = new BaseClass();

obj.add(2,3); obj.add(55,5,31);

float res = obj.add(5,3.4f,45);

System.Console.WriteLine("Addition is : " + res);

Ques 19 : Write a program to implement method overriding


using System;
namespace CompileTimePolymorphism {

class BaseClass {

public virtual void fun() {

System.Console.WriteLine("Method fun of BaseClass");

21
}

class ChildClass : BaseClass{

public override void fun() {

System.Console.WriteLine("Method fun of ChildClass");

class MethodOverriding {

static void Main() {

ChildClassobj = new ChildClass();

obj.fun();

Ques 20 : Write a program in C sharp to create a calculator in windows form.


using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data; usingSystem.Drawing;
usingSystem.Linq; usingSystem.Text;
usingSystem.Windows.Forms;
namespace calculator
{
publicpartialclassForm1 : Form
{
public Form1()
{

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

privatevoid button1_Click(object sender, EventArgs e)


{
SqlConnection con = newSqlConnection("server = ADMIN-PC\\SQLEXPRESS; Integrated Security = true; database
= master");
SqlCommandcmd = newSqlCommand("insert into data(id, name, address) values('" + textBox1.Text + "','" +
textBox2.Text + "','" + textBox3.Text + "')", con); con.Open();
cmd.ExecuteNonQuery();
con.Close();
textBox1.Text = "";
textBox2.Text ="";
textBox3.Text = "";
MessageBox.Show("Inserted Successfully");
}

privatevoid button2_Click(object sender, EventArgs e)


{
SqlConnection con = newSqlConnection("server = ADMIN-PC\\SQLEXPRESS; Integrated Security = true; database
= master");
SqlCommandcmd = newSqlCommand("delete from data where id ='" + textBox1.Text + "'", con);
con.Open();
cmd.ExecuteNonQuery(); con.Close();
MessageBox.Show("Deleted Successfully");
button4_Click(sender, e);
}

privatevoid button3_Click(object sender, EventArgs e)


{
SqlConnection con = newSqlConnection("server = ADMIN-PC\\SQLEXPRESS; Integrated Security = true; database
= master");
SqlCommandcmd = newSqlCommand("update data set name = '" + textBox2.Text + "', address = '" +
textBox3.Text + "' where id ='" + textBox1.Text + "'", con); con.Open();
cmd.ExecuteNonQuery(); con.Close();
MessageBox.Show("Updated Successfully");
button4_Click(sender, e);
}
}
}

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

privatevoid button1_Click(object sender, EventArgs e)


{

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

privatevoid Form1_Load(object sender, EventArgs e)


{

}
}
}
End …

26

You might also like