You are on page 1of 29

1. Write a C# Sharp program to calculate Root of Quadratic Equation.

Using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LAB_IA
{
internal class Quadrati_Euqation
{
public static void Main()
{
int a, b, c;
double d, x1, x2;
Console.Write("\n\n");
Console.Write("Calculate the root of Quadratic Equation");
Console.Write("-------");
Console.Write("\n\n");
Console.Write("Input the Value of a:");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the Value of b:");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the Value of c:");
c = Convert.ToInt32(Console.ReadLine());

d = b * b - 4 * a * c;
if (d == 0)
{
Console.Write("Both roots are equal");
x1 = (-d / (2.0 * a));
x2 = x1;
Console.Write("First Root Root1={0}\n,x1");
Console.Write("First Root Root2={0}\n,x2");
}
else if (d > 0)
{
Console.Write("Both roots are real and diff-2\n");
x1 = d + Math.Sqrt (2 * a);
x2 = -d- Math.Sqrt (2 * a);
Console.Write("First Root Root1={0}\n",x1);
Console.Write("Second Root Root2={0}\n", x2);
}
else
{
Console.Write("Both roots are Imaginary");
}
}
}
}
OUT PUT:
2.Write a C# sharp To count the frequency of each element of an array
using System;
using System.Threading.Tasks;
namespace BCAV404
{
internalclassLAB2
{
publicstaticvoid Main(string[] args)
{
int[] arr1 = newint[100];
int[] fr1 = newint[100];
int n, i, j, ctr;
Console.Write("\n\nCount the frequency of each element of anarray:");
Console.Write("-----------------------------------------------\n");
Console.Write("Input the number of elements to be stored in the 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());
fr1[i] = -1;
}
for (i = 0; i < n; i++)
{
ctr = 1;
for (j = i + 1; j < n; j++)
{
if (arr1[i] == arr1[j])
{
ctr++;
fr1[j] = 0;
}
}
if (fr1[i] != 0)
{
fr1[i] = ctr;
}
}
Console.Write("\n The frequency of all elements of the array :\n");
for (i = 0; i < n; i++)
{
if (fr1[i] != 0)
{
Console.Write("{0} occurs {1} times\n", arr1[i], fr1[i]);
}
}
}
}
Output:
3.write the program in c# sharp to count the total number of alphabets Digits and special
characters in a string
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCAV404
{
internalclassLAB3
{
publicstaticvoid Main()
{
string str;
int alp, digit, splch, i, l;
alp = digit = splch = i = 0;

Console.Write("\n\n Count total number of alphabets,digits and special character:\n");


Console.Write("---------------------------------------------------------\n");
Console.Write("Input the string:");
str = Console.ReadLine();
l = str.Length;
while (i < 1)
{
if ((str[i] >= 'a'&& str[i] <= 'z') || (str[i] >= 'A'&& str[i] <= 'Z'))
{
alp++;
}
elseif (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);
}
}
}
Output:
4.write the program in c# to create a function to calculate the sum of the individual digits
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCAV404
{
internalclassLAB4
{

publicstaticint SumCal(int n)
{
string n1 = Convert.ToString(n);
int sum = 0;
for (int i = 0; i < n1.Length; i++)
sum += Convert.ToInt32(n1.Substring(i, 1));
return sum;
}
publicstaticvoid Main()
{
int num;Console.Write("\n\nFunction:To calculate the sum of the individual digits of a number:\
n");
Console.Write("------------------------------------------------\n");

Console.Write("Enter a number:");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The sum of the digits of the number(0)is:{1}\n", num, SumCal(num));

}
}
}
Output:
5. Create an Application that allows the user to enter a number in the text box name
‘getnum’.Check wether the number in the text box’grtnem’ is palindrome or not.Print the massage
accordingliy in thr label control name lbl display when the user clkicks on the button’check’.
Source Code :
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 Pallindrome
{
publicpartialclasslbl1 : Form
{
publiclbl1()
{
InitializeComponent ();
}

privatevoid check_Click(object sender, EventArgs e)


{
int num, rem, sum = 0;
num = Convert.ToInt32(getnum.Text);
int temp = num;
while (num > 0)

{
rem = num % 10;
sum = (sum * 10) + rem;
num = num / 10;
}
if (temp == sum)

lbl.Text = "The given number is pallindrome";


}
else

{
lbl.Text = "The given number is not pallindrome";
}
}
}
}
Output:

Design part:

Properties:

control name text

Text box getnum --

Label1 lbl1 Enter the Number

Label2 Lbl2 Result

Label3 lbl --

Button Check Check


6. Create an application which whil ask the user to input his name and a message .Display the to
items Concatinated in a label . and Change the format of the label using RedioButtons and
CheckBoxes fur Boxes for selection the user can make the Label Text Bold Under Lined OR Italic
And Change its Color. Include Buttons to display the Message in The Label,Clear the Text Boxes
& Label & Exit.
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 DisplayNameMessage1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
if (chkBold.Checked == true)
{
lblDisplay.Font = new Font(lblDisplay.Font, FontStyle.Bold);
}
if (chkItalic.Checked == true)
{
lblDisplay.Font = new Font(lblDisplay.Font, FontStyle.Italic);
}
if (chkUnderline.Checked == true)
{
lblDisplay.Font = new Font(lblDisplay.Font, FontStyle.Underline);
}
if (rdRed.Checked == true)
{
lblDisplay.ForeColor = Color.Red;
}
if (rdGreen.Checked == true)
{
lblDisplay.ForeColor = Color.Green;
}
if (rdPink.Checked == true)
{
lblDisplay.ForeColor = Color.Pink;
}
lblDisplay.Text = "Name:" + txtName.Text + " " + txtDisplay.Text;
}
}
}

Output:

Propertries:

Control Name Text

textbox txtName ---

Checkbox chkBold BOLD

Checkbox chkItalik ITALIK

Checkbox chkUnderline UNDERLINE

RedioButton rdRed RED

RedioButton rdGreen GREEN

RedioButton rdPink PINK

Label lblName Enter Your Name

Label lblMessage Enter Your Message

Label lblDisplay ----

Button btnDisplay Display Message


7. Draw a square with sides 100 pixels in Length then inscribe a circle of radius 50 inside the
square. Position the square and the inscribed circle in the middle of the screen.
Source Code:
namespace CircleinsideRectangle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Pen red = new Pen(Color.Red);
Pen green = new Pen(Color.Gray);

System.Drawing.SolidBrush fillred = new System.Drawing.SolidBrush(Color.Red);

System.Drawing.SolidBrush fillyellow = new System.Drawing.SolidBrush(Color.Yellow);


Rectangle rect = new Rectangle(100, 100, 100, 100);
Rectangle circle = new Rectangle(100, 100, 100, 100);

private void Form1_Paint(object sender, PaintEventArgs e)


{
Graphics g = e.Graphics;
g.DrawRectangle(red, rect);
g.DrawEllipse(green, circle);

}
}
Output:
08: Write a program that inputs the co-ordinates of three mouse clicks from the user and then
draws a triangle in the output windows using those three points.

Source Code:

using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;

namespace triangle
{
Public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}

privatevoid button1_Click(object sender, EventArgs e)


{
Graphics g = this.CreateGraphics();
g.FillPolygon(Brushes.Blue, new Point[] { new Point(150, 100), new Point(100, 200), new
Point(200, 200) } );
}
}
}
OutPut:

properties:

control Name Text

Button btn Form Triangle


9. Write a program to implement multilevel inheritance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCAV404
{
internalclassLAB5
{
classHuman
{
publicstring name;
publicHuman(string na)
{
name = na;
}
}
classMan : Human
{
publicint age;
publicMan(int age, string name) : base(name)
{
this.age = age;
}
}
classEmployee : Man
{
publicint emp_id;
publicint emp_salary;
publicEmployee(int id, int salary, string name, int age) : base(age, name)
{
emp_id = id;
emp_salary = salary;
}
publicvoid print()
{
Console.WriteLine("Emp ID: " + emp_id);
Console.WriteLine("Emp Name: " + name);
Console.WriteLine("Emp Salary: " + emp_salary);
Console.WriteLine("Emp Age: " + age);
}
staticvoid Main(string[] args)
{
Employee emp = new Employee(101, 1000, "Rahul", 31);
emp.print();
}
}
}
}

Output:
10. C# Program to Demonstrate I List Inteface.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListInterface_2
{
internal class ListInterFace2
{
static void Show(IList<string> list)
{
foreach (string str in list)
{
Console.WriteLine("\t"+ str);
}
}

static void Main()


{
string[] subjects = { "os", "dms", "cn" };
List<string> data = new List<string>();

data.Add("nikhita");
data.Add("Aishwarya");
data.Add("Anju");

Console.WriteLine("Subjects Name:");
Show(subjects);
Console.WriteLine("Student Name:");
Show(data);

}
}
}
Output:
11.Write a program to Display the selected date in the calendar
Source code:

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 Calendar1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
txtdate.Text = monthCalendar1.SelectionRange.Start.Date.ToString();
monthCalendar1.Visible = false;
}
}
}
Output:
Properties:

Control Name Text

Textbox txtDate --

Button --- Select a Date

monthCalendar --- ---


12. Write a program to insert the data in to database using execute Non Query.

Source Code:

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data;
usingMySql.Data.MySqlClient;

namespaceStudent_Info
{
publicpartialclassinfo : System.Web.UI.Page
{
protectedvoidPage_Load(object sender, EventArgs e)
{

protectedvoidbtnInsert_Click(object sender, EventArgs e)


{
MySqlConnection con = newMySqlConnection("server=localhost; user id=root; password=;
database=db_user; pooling=false;");

MySqlCommandcmd = newMySqlCommand("INSERT INTO info (name, add1, add2, email) VALUES


(@name,@add1,@add2,@email)", con);

cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@add1", txtAdd1.Text);
cmd.Parameters.AddWithValue("@add2",txtAdd2.Text);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
con.Open();
introwsAffected = cmd.ExecuteNonQuery();
Label1.Text = "Data inserted into database successfully";
con.Close();
}
}
Output:

Properties:

Control ID Text

Label1 lblDisplay

TextBox1 txtName

TextBox2 txtAdd1

TextBox3 txtAdd2

TextBox4 txtEmail

Button btnInsert Insert


13.Write a program to display the Contact Number of an author using database.

Source Code:

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data;
usingMySql.Data.MySqlClient;

namespaceStudent_Info
{
publicpartialclassinfo : System.Web.UI.Page
{
protectedvoidPage_Load(object sender, EventArgs e)
{

protectedvoidbtnInsert_Click(object sender, EventArgs e)


{
MySqlConnection con = newMySqlConnection("server=localhost; user id=root; password=;
database=db_user; pooling=false;");

MySqlCommandcmd = newMySqlCommand("select contact from user where name=@name", con);

cmd.Parameters.AddWithValue("@name", txtAuthName.Text);

con.Open();
Label1.Text = "Connection Successful";
MySqlDataReaderrd= cmd.ExecuteReader();

GridView1.DataSource = rd;
GridView1.DataBind();

con.Close();
}
}
}
Output:

Properties :

Controls ID Text

TextBox1 txtAuthName

Button1 btnSearch Search

GridView1 GridView1

Label1 lblDisplay

You might also like