0% found this document useful (0 votes)
7 views28 pages

c# program

The document outlines various C# programming exercises, including sorting numbers, displaying Floyd's triangle, managing student details, calculating areas and volumes using structures, enumerators, inheritance, polymorphism, operator overloading, and creating a calculator using Windows Forms. Each exercise includes an aim, algorithm, and program code demonstrating the implementation. The results indicate successful execution of each program.

Uploaded by

Jc.Raaja b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views28 pages

c# program

The document outlines various C# programming exercises, including sorting numbers, displaying Floyd's triangle, managing student details, calculating areas and volumes using structures, enumerators, inheritance, polymorphism, operator overloading, and creating a calculator using Windows Forms. Each exercise includes an aim, algorithm, and program code demonstrating the implementation. The results indicate successful execution of each program.

Uploaded by

Jc.Raaja b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 28

Ex.No.

1
Date :
SORTING

Aim:
To write a C# program for sorting list of numbers.

Algorithm:
Step 1: Start the process.
Step 2: Get the list of numbers from the users.
Step 3: Sort the numbers using array.
Step 4: Display the sorted numbers.
Step 5: Stop the process.

Program:

using System;
using System.Collections.Generic;
using System.Text;
namespace sorting
{
class Program
{
static void Main(string[] args)
{
System.Console.Write("Enter No of Elements :");
int n = int.Parse(System.Console.ReadLine());
int[] arr = new int[n];
int i,j,temp;
for (i = 0; i < n; i++)
{
System.Console.Write("Enter value for Element arr[{0}] : ",i);
arr[i] = int.Parse(System.Console.ReadLine() );
}
System.Console.WriteLine();
System.Console.WriteLine("\nUnsorted List");
foreach (int x in arr)
{
System.Console.Write(" {0} ", x);
}
System.Console.WriteLine();
for(i=0 ;i<n;i++)
for(j=i;j<n;j++)

1
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
System.Console.WriteLine("\nSorted List");
foreach (int x in arr)
{
System.Console.Write(" {0} ", x);
}
System.Console.WriteLine();
System.Console.WriteLine();
}
}
}

Output

Result
Thus C# program for sorting list of numbers is sorted successfully.

2
Ex.No.2
Date.
FLOYDS TRIANGLE

Aim:
To write a C# program for displaying Floyds triangle.

Algorithm:
Step 1: Start the process.
Step 2: Get the number of line from the users
Step 3: Print the ones and zeros alternatively.
Step 4: Display the lines such a way that forms of triangle.
Step 5: Stop the process.

Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace ftriangle
{
class Program
{
static void Main(string[] args)
{
int n=5, i, j;
System.Console.Write("N=");
n = int.Parse(System.Console.ReadLine() );
for (i = 1; i <= n; i++)
{
for (j = i; j>0; j--)
System.Console.Write("{0} ", (j % 2));
System.Console.WriteLine();
}
System.Console.WriteLine();

3
for (i = n; i>=0; i--)
{
for (j = i; j > 0; j--)
System.Console.Write("{0} ", (j % 2));
System.Console.WriteLine();
} } }}

Output

Result
Thus a C# program for Floyds triangle is displayed successfully.

4
Ex.No.3
Date
STUDENT DETAILS USING CLASS

Aim:
To write a C# program for the student details using class.

Algorithm:
Step 1: Start the process.
Step 2: Get the inputs related to the students.
Step 3: Calculate the total marks.
Step 4: Calculate the average.
Step 5: Calculate the result whether it is pass or fail.
Step 6: Display the result.
Step 7: Stop the process.

Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace studetail
{
class Program
{
static void Main(string[] args)
{
Stu o = new Stu();
o.Idata();
o.Odata();
}
}
class Stu
{
long Regno;
5
int Eng, Tam, Mat;
float Total, Avg;
public bool pf;
public Stu()
{
pf = false;
Eng = Tam = Mat = 0;
Total = Avg = 0;
}
void StuResult()
{
if (Eng >= 50 && Tam >= 50 && Mat >= 50)
pf = true;
}
void StuTotal()
{
Total = Eng + Tam + Mat;
}
void StuAvg()
{
Avg = ((Eng + Tam + Mat)/3.0f);
}
public void Idata()
{
System.Console.Write("Enter The Reg No. : ");
Regno = long.Parse(System.Console.ReadLine());
System.Console.Write("\nEnter English Mark : ");
Eng = int.Parse(System.Console.ReadLine());
System.Console.Write("\nEnter Tamil Mark : ");
Tam = int.Parse(System.Console.ReadLine());
System.Console.Write("\nEnter Maths Mark : ");
Mat = int.Parse(System.Console.ReadLine());
}
public void Odata() {
StuResult();

6
StuTotal();
StuAvg();
System.Console.WriteLine();
System.Console.WriteLine("Regno :{0}", Regno);
System.Console.WriteLine("English :{0}", Eng);
System.Console.WriteLine("Tamil :{0}", Tam);
System.Console.WriteLine("Maths :{0}", Mat);
System.Console.WriteLine("Result :{0}",(pf ? "Pass" :"Fail" ));
if (pf)
{ System.Console.WriteLine("\nTotal :{0}", Total );
System.Console.WriteLine("\nAvg :{0}", Avg);
} } }}

Output

Result
Thus a C# program for the student details using class is done successfully.

7
Ex.No.4
Date
STRUCTURES

Aim:
To write a C# program to find out volumes of rectangle and square using
structures.

Algorithm:
Step 1: Start the process.
Step 2: Declare a shape structure to calculate area and volume.
Step 3: Get the inputs for rectangle.
Step 4: Calculate the area and volume for rectangle.
Step 5: Get the inputs for square.
Step 6: Calculate the area and volume for square.
Step 7: Display the results.
Step 8: Stop the process.

Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace ssss
{ struct shape
{ int l, b, h, s;
public shape(int i)
{ System.Console.Write("l=");
l = int.Parse(System.Console.ReadLine());
System.Console.Write("b=");
b = int.Parse(System.Console.ReadLine());
System.Console.Write("h=");
h = int.Parse(System.Console.ReadLine());
System.Console.Write("s=");

8
s = int.Parse(System.Console.ReadLine()); }
public int Rectarea()
{ return (l * b); }
public int Rectvolume()
{ return (l * b * h); }
public int sqrarea()
{ return (s*s); }
public int sqrvolume()
{ return (s * s * s); }
public void Display()
{ Console.WriteLine("\n\nRectancle\n~~~~~~~~~\nArea = " + Rectarea());
Console.WriteLine("Volume = " + Rectvolume());
Console.WriteLine("\n\nSquare\n~~~~~~\nArea =" + sqrarea());
Console.WriteLine("Volume=" + sqrvolume());
} }
class Testrectangle
{ public static void Main()
{ shape shaba = new shape(0);
shaba.Display(); } }}

Output

Result
Thus a C# program is to find out volumes of rectangle and square is created
successfully using structures.

9
Ex.No.5
Date
ENUMERATOR

Aim:
To Write a C# program to find out the shape of the area using enumerator.

Algorithm:
Step 1: Stat the process.
Step 2: Declare the circle of the area.
Step 3: Declare the square of the area.
Step 4: Display using switch case.
Step 5: Display the output.
Step 6: Stop the process.

Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace enumerator
{
class Area
{
public enum Shape
{
Circle, Square
}
public void AreaShape (int x, Shape shape)
{
double area;
switch (shape)
{
case Shape.Circle:
area = Math.PI * x*x;

10
Console.WriteLine("\nCircle Area = " + area);
break;
case Shape.Square:
area = x*x;
Console.WriteLine("\nSquare Area = " + area);
break;
default:
Console.WriteLine("\nInvalied Input\n");
break;
}
}
}
class Enumtest
{
public static void Main()
{
System.Console.Write("Enter the value of n =");
int n = int.Parse(System.Console.ReadLine());
Area area = new Area();
area.AreaShape(n,Area.Shape.Circle);
area.AreaShape(n,Area.Shape.Square);
area.AreaShape(n,(Area.Shape) 1);
area.AreaShape(n,(Area.Shape) 10);
}
}
}

11
Output

Result
Thus a C# program is to find out the shape of the area is created successfully
using enumerator.

Ex.No.6

12
Date.
INHERITANCE

Aim:
To write a C# program calculate sum and average using inheritance.

Algorithm:
Step 1: Start the process.
Step 2: Get the list of numbers from the users.
Step 3: Define the array for the list of numbers.
Step 4: Find out sum and average to the list of numbers.
Step 5: Print the result.
Step 6: Stop the process.

Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace Int
{
class Program
{
static void Main(string[] args)
{
B o = new B();
o.Idata();
o.calc();
o.Odata();
} }
class A
{
protected int[] arr = new int[5];
protected int sum;
public A()

13
{ sum = 0; }
public void Idata()
{ int i = 0;
for (i = 0; i < 5; i++)
{
System.Console.Write("Enter the value of Element {0}/{1}=", (i + 1), 5);
arr[i] = int.Parse(System.Console.ReadLine());
} } }
class B : A
{ float avg=0;
public void calc()
{ int i;
for (i = 0; i < 5; i++)
{ sum = sum + arr[i]; }
avg = sum / 5.0f;
}
public void Odata()
{ System.Console.WriteLine("\n\nArray List");
System.Console.WriteLine("~~~~~~~~~~\n");
for (int i = 0; i < 5; i++)
System.Console.Write(" {0}",arr[i]);
System.Console.WriteLine("\n\nSum : {0}\n", sum);
System.Console.WriteLine("\nAvg : {0}\n", avg);
}
}
}

14
Output

Result
Thus a C# program of sum and average is calculated successfully using
inheritance.

15
Ex.No.7
Date.
POLYMORPHISM

Aim:
To write a C# program for calculating area and volume of shape using
overriding in polymorphism.

Algorithm:
Step 1: Start the process.
Step 2: Get data of square and rectangle
Step 3: Calculate the area and volume of square.
Step 4: Calculate the area and volume of rectangle.
Step 5: Print the result.
Step 6: Stop the process.

Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace poly
{
class Program
{
static void Main(string[] args)
{
Sqr s = new Sqr(50f);
s.Calc();
s.Odata();
Rect o = new Rect(10f, 20f, 30f);
o.Calc();
o.Odata();
} }
class Sqr
16
{ protected float a;
float area,vol;
public Sqr() { }
public Sqr(float x)
{ a = x; }
public virtual void Calc ()
{ area = a * a;
vol = a*a*a ; }
public virtual void Odata()
{
System.Console.WriteLine("\n\nSide of Square is {0}.", a);
System.Console.WriteLine("\n\nVolume of square is {0}.", vol);
System.Console.WriteLine("\n\nArea of Square is {0}.", area);
}
}
class Rect : Sqr
{
float area, vol,l,b,w;
public Rect() { }
public Rect(float x, float y,float z):base(x)
{ l = x; b = y; w = z; }
public override void Calc()
{ area = l * b;
vol = l * b * w;
base.Calc(); }
public override void Odata()
{ System.Console.WriteLine("\n\nRect \n~~~~\nl={0}\nb={1}\nw={2} ", l,b,w);
System.Console.WriteLine("\n\nRect Volume is {0}.", vol);
System.Console.WriteLine("\n\nRect Area is {0}.", area);
base.Odata(); } }}

17
Output

Result
Thus a C# program for area and volume of shape is calculated successfully
using overriding in polymorphism.

18
Ex.No.8
Date
OPERATOR OVERLOADING

Aim:
To write a C# program using Operator Overloading.

Algorithm:
Step 1: Start the process.
Step 2: Create an operator overloading in a class for addition and subtraction.
Step 3: Get the input form the user.
Step 4: Call the operator overloading functions using objects.
Step 5: Display the output of addition and subtraction.
Step 6: Stop the process.

Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace operator_overloading
{ class Complex
{
double x;
double y;
public Complex() { }
public Complex(double real, double img)
{ x = real;
y = img; }
public static Complex operator +(Complex c1, Complex c2)
{ Complex c3 = new Complex();
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return (c3); }
public static Complex operator -(Complex c1, Complex c2)
19
{ Complex c3 = new Complex();
c3.x = c1.x - c2.x;
c3.y = c1.y - c2.y;
return (c3); }
public void Display()
{ Console.Write("\nX="+x);
Console.Write("\nY="+ y);
Console.WriteLine();
} }
class ComplexTest
{
public static void Main()
{
Complex a, b, c,d;
a = new Complex(5.5, 3.5);
b = new Complex(4.5, 6.5);
c = a + b;
Console.Write("\n\nObject a:");
a.Display();
Console.Write("\n\nObject b:");
b.Display();
Console.Write("\n\nAdd Object c:");
c.Display();
d = a - b;
Console.Write("\n\nSub Object d:");
d.Display();
}
}
}

20
Output

Result

Thus a C# program using Operator Overloading is created successfully.

21
Ex.No.9
Date.
WINDOWS APPLICATION

Aim:
To write a C# program for calculator using windows form designs.

Algorithm:
Step 1: Start the process.
Step 2: Design the form using tools as calculator.
Step 3: Define the functionalities to the each element in the form.
Step 4: Check the functions of the calculator.
Step 5: Stop the process.

Program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace win
{
public partial class Form1 : Form
{
int op;
public Form1()
{ InitializeComponent(); }
private void button3_Click(object sender, EventArgs e)
{ clacinput(3); }
private void button1_Click(object sender, EventArgs e)
{ clacinput(1); }

22
private void clacinput(int n)
{
if (textBox1.Text == "")
textBox1.Text = n.ToString() ;
else
textBox1.Text = ((int.Parse(textBox1.Text) * 10) + n).ToString();
}
private void button2_Click(object sender, EventArgs e)
{ clacinput(2); }
private void button4_Click(object sender, EventArgs e)
{ clacinput(4); }
private void button5_Click(object sender, EventArgs e)
{ clacinput(5); }
private void button6_Click(object sender, EventArgs e)
{ clacinput(6); }
private void button7_Click(object sender, EventArgs e)
{ clacinput(7); }
private void button8_Click(object sender, EventArgs e)
{ clacinput(8); }
private void button9_Click(object sender, EventArgs e)
{ clacinput(9); }
private void button10_Click(object sender, EventArgs e)
{ textBox1.Text = "0"; }
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'studetailDataSet.stud' table. You can
move, or remove it, as needed.
textBox1.Text = "0";
}
private void button12_Click(object sender, EventArgs e)
{ label1.Text = textBox1.Text;
textBox1.Text = "0";
op = 0;
}
private void button13_Click(object sender, EventArgs e)

23
{
if (op == 0)
{
textBox1.Text = (int.Parse(label1.Text) + int.Parse(textBox1.Text)).ToString();
label1.Text = "";
}
if (op == 1)
{
textBox1.Text = (int.Parse(label1.Text) - int.Parse(textBox1.Text)).ToString();
label1.Text = "";
}
if (op == 2)
{
textBox1.Text = (int.Parse(label1.Text) * int.Parse(textBox1.Text)).ToString();
label1.Text = "";
}
if (op == 3)
{
textBox1.Text = (int.Parse(label1.Text) / int.Parse(textBox1.Text)).ToString();
label1.Text = "";
}
}

private void button14_Click(object sender, EventArgs e)


{
label1.Text = textBox1.Text;
textBox1.Text = "0";
op = 3;
}
private void button15_Click(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
textBox1.Text = "0";
op = 1;
}

24
private void button16_Click(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
textBox1.Text = "0";
op = 2;
}
private void button11_Click(object sender, EventArgs e)
{
label1.Text = "";
textBox1.Text = "0";
}
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = (int.Parse(textBox1.Text) * 10).ToString();
}
}
}

Output

25
Result
Thus a C# program for calculator using windows form designs is done
successfully.

26
Ex.No.10
Date
DATABASE CONNECTION

Aim:
To write a C# program for connect a database using student details.

Algorithm:
Step 1: Start the process.
Step 2: Open the new windows application form.
Step 3: Create the database using SQL database.
Step 4: Create the new database table.
Step 5: Connect to the database and table.
Step 6: Display the table.
Step 7: Stop the process.

Form Design

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

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

private void studBindingNavigatorSaveItem_Click(object sender, EventArgs e)


{
this.Validate();
this.studBindingSource.EndEdit();
this.studTableAdapter.Update(this.studetailDataSet.stud);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'studetailDataSet.stud' table. You can
move, or remove it, as needed.
this.studTableAdapter.Fill(this.studetailDataSet.stud);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
totalTextBox.Text =( int.Parse(m1TextBox.Text) + int.Parse(m2TextBox.Text) +
int.Parse(m3TextBox.Text)).ToString() ;
avgTextBox.Text = (float.Parse(totalTextBox.Text)/3.0f).ToString();
}
private void totalTextBox_TextChanged(object sender, EventArgs e)
{ } }}

28

You might also like