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

C Sharp Lab

The document contains multiple C# and VB.NET code examples demonstrating various programming concepts such as conditional statements, control statements, Windows controls, multi-threading, subroutines, and database connectivity. It also includes examples for prime number checking, factorial calculation, palindrome checking, and summing digits. Each section provides a practical implementation of the concepts in console or Windows applications.

Uploaded by

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

C Sharp Lab

The document contains multiple C# and VB.NET code examples demonstrating various programming concepts such as conditional statements, control statements, Windows controls, multi-threading, subroutines, and database connectivity. It also includes examples for prime number checking, factorial calculation, palindrome checking, and summing digits. Each section provides a practical implementation of the concepts in console or Windows applications.

Uploaded by

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

Part A:

1. Conditional stmt(C# -CONSOLE)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program1
{
static void Main(string[] args)
{
int x;
int a, b;
Console.WriteLine("Enter any two nymbers");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your choice");
Console.WriteLine("1. Simple if statement");
Console.WriteLine("2. if else statement");
Console.WriteLine("3. else if statement");
Console.WriteLine("4. Exit");
x = Convert.ToInt32(Console.ReadLine());
switch (x)
{
case 1:
if (a < b)
{
Console.WriteLine("a is smallest");
}
break;
case 2:
if (a > b)
Console.WriteLine("a is largest");
else
Console.WriteLine("b is largest");
break;
case 3:
if (a == b)
Console.WriteLine("a is equal to b");
else if (a > b)
Console.WriteLine("a greater than b");
else
Console.WriteLine("b greater than a");
break;
case 4:
Console.WriteLine("Exit");
break;
default:
Console.WriteLine("invalid choice");
break;
}
Console.ReadKey();
}
}
}

2. control stmts (C# -CONSOLE)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace loopingstatement
{
class Program2
{
static void Main(string[] args)
{
int x;
int i;
int a = 1;
Console.WriteLine("Enter your choice");
Console.WriteLine("1. For loop");
Console.WriteLine("2. While loop");
Console.WriteLine("3. Do loop");
Console.WriteLine("4. For each loop");
x = Convert.ToInt32(Console.ReadLine());
switch (x)
{
case 1:

Console.WriteLine("First 10 Natural Numbers are: ");


for (i = 1; i <= 10; i++)
Console.WriteLine(i);
break;
case 2:
while (a <= 10)
{
Console.WriteLine(a);
a++;
}
break;
case 3:
do
{
Console.WriteLine(a);
a++;
}
while (a <= 10);
break;
case 4:
char[] myArray = { 'H', 'e', 'l', 'l', 'o' };
foreach (char ch in myArray)
{
Console.WriteLine(ch);
}
break;
default:
Console.WriteLine("invalid choice");
break;
}
Console.ReadKey();
}
}
}

3. Windows control (C# -WINDOWS)

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string gender, qualification;
if (radioButton1.Checked)
gender = "Male";
else
if (radioButton2.Checked)
gender = "FeMale";

else if (radioButton3.Checked)
gender = "Other";
else gender = "Gender not selected.";
if (checkBox1.Checked)
qualification = "Post Graduate";
else if (checkBox2.Checked)
qualification = "Graduate";
else if (checkBox3.Checked)
qualification = "PUC";
else qualification = "Not Checked";
MessageBox.Show("Name : " + textBox1.Text + "\nAddress : " +
textBox2.Text + "\nDOB :" + dateTimePicker1 + "\nGender : " +
gender + "\nState : " + comboBox1.Text + "\nQualification : " +
qualification);
}

private void button2_Click_1(object sender, EventArgs e)


{
Close();
}
}
}

4. Multi threading (C# -CONSOLE)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApp4
{
class Program4
{
public static void method1()
{
// It prints numbers from 0 to 10
for (int I = 0; I <= 10; I++)
{
Console.WriteLine("Method1 is : {0}", I);
// When the value of I is equal to 5 then
// this method sleeps for 6 seconds
if (I == 5)
{
Thread.Sleep(6000);
}
}
}

// static method two


public static void method2()
{
// It prints numbers from 0 to 10
for (int J = 0; J <= 10; J++)
{
Console.WriteLine("Method2 is : {0}", J);
}
}
static void Main(string[] args)
{
// Creating and initializing threads
Thread thr1 = new Thread(method1);
Thread thr2 = new Thread(method2);
thr1.Start();
thr2.Start();
}
}
}

5. Subroutines and function(C# -CONSOLE)

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

namespace funsubdemo
{
class Program
{

//this is a subroutine
void AddValue(int a, int b)
{
Console.WriteLine("Value of a is: " + a);
Console.WriteLine("Value of b is: " + b);

Console.WriteLine("Sum: {0}", a + b);

//this is a method
int AddValues(int x, int y)
{
return x + y;
}

static void Main(string[] args)


{
int a = 100, b = 200;
Program obj1 = new Program();
obj1.AddValue(a, b);

Console.WriteLine("The sum of a and b is " +


obj1.AddValues(a, b));

Console.ReadKey();
}
}
}
6. Application- BUILT IN FUN(vb.net)

Public Class Form1

Private Sub Button1_Click(ByVal sender As Object, ByVal e As


EventArgs) Handles Button1.Click
Dim s As String
s = "welcome visual studio world"
Dim i As Integer
i = Len(s)
MsgBox(i)
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button2.Click
Dim i As Integer
i = Asc("B")
MsgBox(i)
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button3.Click
Dim i As String
i = Chr(66)
MsgBox(i)
End Sub
Private Sub Button4_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button4.Click
Dim i As Boolean
Dim s As String
s = "welcome visual studio world"
i = IsNumeric(s)
MsgBox(i)
End Sub
Private Sub Button5_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button5.Click
Dim i As Boolean
Dim s As String
s = "30.56"
i = IsNumeric(s)
MsgBox(i)
Dim decprice As Decimal
decprice = CDec(s)
MsgBox(decprice)
End Sub
End Class
7. MDI- Employee payroll(vb.net)

MDI coding
Public Class Form2

Private Sub EmployeeDetailsToolStripMenuItem_Click(ByVal


sender As System.Object, ByVal e As System.EventArgs) Handles
EmployeeDetailsToolStripMenuItem.Click
Form1.Show()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ExitToolStripMenuItem.Click
End

End Sub
End Class
Child Class Coding
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles Button1.Click
Dim netpay As Double
Dim gross As Double
Dim week, hour, sun, sat, sathour, sunhour As Double
Dim dec, tax As Double
week = Convert.ToDouble(txtweek.Text) * 125
sun = Convert.ToDouble(txtsat.Text) * 80
sat = Convert.ToDouble(txtsun.Text) * 160
hour = Convert.ToDouble(txthrweek.Text) * 180
sathour = Convert.ToDouble(txthrsat.Text) * 30
sunhour = Convert.ToDouble(txtharsun.Text) * 30
gross = (week + hour + sun + sat + sathour + sunhour)
dec = gross * (10 / 100)
tax = gross * (15 / 100)
netpay = gross - tax - dec
txtgross.Text = Convert.ToString(gross)
txtdec.Text = Convert.ToString(dec)
txttax.Text = Convert.ToString(tax)
total.Text = Convert.ToString(netpay)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles Button2.Click
End
End Sub
End Class

8. Console to demonstrate OOP Concept (C# -CONSOLE)

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

class Program
{
void print(int i, int j)
{
Console.WriteLine("Printing int: {0}", (i + j));
}
void print(string a, string b)
{
Console.WriteLine("Printing String " + a + b);
}
static void Main(string[] args)
{
Program prog = new Program();

// Call print for sum of integers


prog.print(5, 6);

// Call to concatenate strings


prog.print("Hello", "World");

Hyundai hyn = new Hyundai();


String descp = hyn.Describe();

Console.WriteLine(descp);
Console.ReadKey();
}
}

abstract class Car


{
public virtual string Describe()
{
return "Description of the car";
}
}

class Hyundai : Car


{
public override string Describe()
{
return "Description of the car is now Hyundai";
}
}

9.Dynamic Login Processing- Web application(vb.net)

Imports System.Data.OleDb

Public Class _Default


Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As


System.EventArgs) Handles Me.Load

End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button1.Click
Dim con As New
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\indo\Desktop\rach-log\login.accdb")
Dim cmd As OleDbCommand = New OleDbCommand("select *
from table1 where username='" + TextBox1.Text & "' and
password='" + TextBox2.Text + "'", con)
con.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
If dr.Read() = True Then
MsgBox("login success")
Else
MsgBox("invalid")
End If

End Sub
End Class
10. Windows application- database connectivity for core banking
transaction(vb.net)
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the
'BanktransactionnewDataSet.bank' table. You can move, or remove it,
as needed.
Me.BankTableAdapter.Fill(Me.BanktransactionnewDataSet.bank)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles Button1.Click
BankBindingSource.AddNew()

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button2.Click

On Error GoTo SaveError

BankBindingSource.EndEdit()
BankTableAdapter.Update(BanktransactionnewDataSet.bank)
MsgBox("Data has been saved")

SaveError:
Exit Sub

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles Button3.Click
Me.Close()

End Sub
End Class
Part B
1. Prime using vb.net

Public Class Form1


Private Sub Button1_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button1.Click
Dim check As Integer
check = 1
Dim num As Integer
num = TextBox1.Text
For i = 2 To (num - 1)
If num Mod i = 0 Then
check = 0
Exit For
End If
Next
If check = 0 Then
MessageBox.Show("It is Not a prime")
Else
MessageBox.Show("It is a prime")
End If
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button2.Click
End
End Sub
End Class

2. VB.NET program to find factorial of a given number


Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button1.Click
Dim n, f, i As Integer
f=1
n = Val(TextBox1.Text)
For i = 1 To n
f=f*i
Next
MsgBox("Factorial is :" & f, vbInformation + vbOKCancel,
"Factorial")
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button2.Click
End
End Sub
End Class

3. Prime Number (C# -CONSOLE)

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

namespace ConsoleApplication22
{
public class PrimeNumber
{
public static void Main(string[] args)
{
int n, i, m = 0, flag = 0;
Console.Write("Enter the Number to check Prime: ");
n = int.Parse(Console.ReadLine());
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
Console.Write("Number is not Prime.");
flag = 1;
break;
}
}
if (flag == 0)
Console.Write("Number is Prime.");
Console.ReadKey();
}
}

}
4. Palindrome(C# -CONSOLE)

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

namespace ConsoleApplication22
{
public class Palindrome
{
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 * 10) + r;
n = n / 10;
}
if (temp == sum)
Console.Write("Number is Palindrome.");
else
Console.Write("Number is not Palindrome");
Console.ReadKey();
}
}

5. Sum of digits program (C# -CONSOLE)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication22
{
public class Sumdigit
{
public static void Main(string[] args)
{
int n, sum = 2, m;

Console.Write("Enter a number: ");


n = int.Parse(Console.ReadLine());
while (n > 0)
{
m = n % 10;
sum = sum + m;
n = n / 10;
}
Console.Write("Sum is= " + sum);
Console.ReadKey();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy