0% found this document useful (0 votes)
11 views

C# Lab Pgms

Uploaded by

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

C# Lab Pgms

Uploaded by

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

USN No.

_________________________ Subject: C# & Dot Net Framework Lab

LAB 1A: Write a C# program to show the machine details like machine
name, Operating System, Version, Physical Memory and calculate
time since the Last Boot Up.

using System;

namespace LabPgm1
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("\n1. Amount of physical memory mapped to the process


context: " + Environment.WorkingSet);
Console.WriteLine("2. MachineName: {0}", Environment.MachineName);
Console.WriteLine("3. The current operating System version is " +
Environment.OSVersion);
Console.WriteLine("4. System uptime is " + (Environment.TickCount*0.001) + "
seconds");
Console.ReadLine();

}
}
}

OUTPUT

1. Amount of physical memory mapped to the process context: 24543232


2. MachineName: COM001
3. The current operating System version is Microsoft Windows NT 6.2.9200.0
4. System uptime is 77112.765 seconds

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 2A: Write a C# program to count a total number of alphabets, digits


and special character in a string

using System;

namespace LabPgm2
{
class Program
{
static void Main(string[] args)
{
string str;
int alp, digit, splch, i, l;
alp = digit = splch = i = 0;

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


Console.Write("--------------------------------------------------------------------\n");

Console.Write("Input the string : ");


str = Console.ReadLine();

l = str.Length;
while (i < l)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
alp++;
}
else if (str[i] >= '0' && str[i] <= '9')
{
digit++;
}
else
{
splch++;
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

i++;
}

Console.Write("\n\nNumber 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);

Console.ReadLine();
}
}
}

OUTPUT

Count total number of alphabets, digits and special characters :


--------------------------------------------------------------------
Input the string : Hello123 Welcome @ Jain College.

Number of Alphabets in the string is : 23


Number of Digits in the string is : 3
Number of Special characters in the string is : 6

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 3A: Write a C# program to create a function to calculate the sum of


the individual digits of a given number

using System;

namespace LabPgm3
{
class Program
{
static public int calc_sum(int n)
{
int s=0,m;
while (n > 0)
{
m = n % 10;
s = s + m;
n = n / 10;
}
return s;
}
static void Main(string[] args)
{
int n, sum = 0, m;
Console.Write("Enter a number: ");
n = int.Parse(Console.ReadLine());

sum = calc_sum(n);

Console.Write("Sum is= " + sum);


Console.ReadKey();
}
}
}

OUTPUT

Enter a number: 1234


Sum is= 10

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 4A: 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 middle of the screen.

STEPS:
Go to File -> Click on New -> Project -> Select Windows Forms Application -> Click
OK
Once Windows Form Created
Go to Properties Table -> Click on Events -> Double Click on Paint -> Write 5 Lines
code as given
below in source code.

using System;
using System.Drawing;
using System.Windows.Forms;

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

private void Form1_Paint(object sender, PaintEventArgs e)


{
Graphics g = e.Graphics;
int w = this.ClientSize.Width;
int h = this.ClientSize.Height;
g.DrawRectangle(Pens.Red, w / 2 - 50, h / 2 - 50, 100, 100);
g.DrawEllipse(Pens.Green, w / 2 - 50, h / 2 - 50, 100, 100);
}
}
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

OUTPUT

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 5A: Write a C# program to implement multilevel inheritance

using System;

namespace LabPrm5
{
public class Brand
{
public string Name;
public void GetName()
{
Console.WriteLine("Mobile Name:{0}", Name);
}
}

public class Model : Brand


{
public string Mod;
public void GetModel()
{
Console.WriteLine("Mobile Model={0}", Mod);
}
}

public class Price : Model


{
public double Rate;
public double Disc;
public void GetPrice()
{
Rate = Rate - Disc;
Console.WriteLine("Total Amount={0}", Rate);
}
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

class Program
{
static void Main(string[] args)
{
Price p = new Price();

Console.WriteLine("Enter Mobile details");


Console.WriteLine("Enter Brand Name:");
p.Name = Console.ReadLine();

Console.WriteLine("Enter Model:");
p.Mod = Console.ReadLine();

Console.WriteLine("Enter Mobile Price");


p.Rate = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter Discount amount");


p.Disc = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("........................");
Console.WriteLine("Mobile details are:");
p.GetName();
p.GetModel();
p.GetPrice();

Console.ReadLine();
}
}
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

OUTPUT

Enter Mobile details


Enter Brand Name:
Samsung
Enter Model:
Galaxy A14
Enter Mobile Price
18999
Enter Discount amount
10

........................
Mobile details are:
Mobile Name:Samsung
Mobile Model=Galaxy A14
Total Amount=18989

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 6A: Write a C# program to demonstrate System exception

using System;

namespace LabPgm6
{
class Program
{
static void Main(string[] args)
{
int Number1, Number2, Result=0;
try
{
Console.WriteLine("Enter First Number");
Number1 = int.Parse(Console.ReadLine());

Console.WriteLine("Enter Second Number");


Number2 = int.Parse(Console.ReadLine());

Result = Number1 / Number2;


}

catch (DivideByZeroException)
{
Console.WriteLine("\nSecond Number Should Not Be Zero");
}

catch (FormatException fe)


{
Console.WriteLine("\nEnter Only Integer Numbers");
}

catch (Exception)
{
Console.WriteLine("\nGeneric Catch Block...");
}

finally
{
Console.WriteLine("\nResult:" + Result);
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

Console.ReadKey();
}
}
}

OUTPUT
Enter First Number
4
Enter Second Number
2

Result: 2

Enter First Number


6
Enter Second Number
0

Second Number Should Not Be Zero


Result: 0

Enter First Number


8
Enter Second Number
9.6

Enter Only Integer Numbers


Result: 0

Enter First Number


j

Enter Only Integer Numbers


Result: 0

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 7A: Write an object oriented program to demonstrate bank


transaction. Include methods for amount deposit, amount withdraw
and display

using System;

namespace LabPgm7
{
class Bank
{
public int balance(int amount)
{
Console.WriteLine("\n* * * * *YOUR CURRENT BALANCE IS Rs :
{0}",amount);
return amount;
}
public int deposit(int amount)
{
Console.Write("\n* * * * *ENTER THE DEPOSIT AMOUNT: ");
int deposit = int.Parse(Console.ReadLine());
amount = amount + deposit;
Console.WriteLine("\n\n* * * * *YOUR TOTAL BALANCE IS Rs : {0}\n",
amount);
return amount;
}
public int withdraw(int amount)
{
Console.Write("* * * * *ENTER THE WITHDRAW AMOUNT: ");
int withdraw = int.Parse(Console.ReadLine());
if (withdraw <= 100)
{
Console.WriteLine("\n\n* * * * *PLEASE ENTER THE AMOUNT IN ABOVE
100");
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

else if (withdraw > amount)


{
Console.WriteLine("\n\n* * * * *SORRY! INSUFFICENT BALANCE");
}
else
{
amount = amount - withdraw;
Console.WriteLine("\n\n* * * * *PLEASE COLLECT YOUR CASH");
Console.WriteLine("* * * * *CURRENT BALANCE IS Rs : {0}", amount);
return amount;
}
return amount;
}
}
class Program : Bank
{
static void Main(string[] args)
{
int amount = 1000;
int choice, acc = 0;
Program obj = new Program();
Console.WriteLine("Enter Your Account Number ");
acc = int.Parse(Console.ReadLine());
do
{
Console.WriteLine("\n\n*******MENU********");
Console.WriteLine("WELCOME TO BANK");
Console.WriteLine("1. Current Balance");
Console.WriteLine("2. Withdraw");
Console.WriteLine("3. Deposit");
Console.WriteLine("4. Exit");
Console.WriteLine("***************\n\n");

Console.WriteLine("ENTER YOUR CHOICE : ");


choice = int.Parse(Console.ReadLine());

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

switch (choice)
{
case 1: amount = obj.balance(amount);
break;
case 2:
amount = obj.withdraw(amount);
break;
case 3:
amount = obj.deposit(amount);
break;
case 4:
Console.WriteLine("thank you \n");
break;
default:
Console.WriteLine("\nInvalid choice\n");
break;
}
}while (choice != 4);
}
}
}

OUTPUT

Enter Your Account Number


1234

*******MENU********
WELCOME TO BANK
1. Current Balance
2. Withdraw
3. Deposit
4. Exit
***************

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

ENTER YOUR CHOICE :


1
* * * * *YOUR CURRENT BALANCE IS Rs : 1000

*******MENU********
WELCOME TO BANK
1. Current Balance
2. Withdraw
3. Deposit
4. Exit
***************

ENTER YOUR CHOICE : 2


* * * * *ENTER THE WITHDRAW AMOUNT: 100

* * * * *PLEASE ENTER THE AMOUNT IN ABOVE 100

*******MENU********
WELCOME TO BANK
1. Current Balance
2. Withdraw
3. Deposit
4. Exit
***************

ENTER YOUR CHOICE : 2


* * * * *ENTER THE WITHDRAW AMOUNT: 1500

* * * * *SORRY! INSUFFICENT BALANCE

*******MENU********
WELCOME TO BANK
1. Current Balance
2. Withdraw
3. Deposit
JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:
USN No. _________________________ Subject: C# & Dot Net Framework Lab

4. Exit
***************

ENTER YOUR CHOICE : 2


* * * * *ENTER THE WITHDRAW AMOUNT: 500

* * * * *PLEASE COLLECT YOUR CASH


* * * * *CURRENT BALANCE IS Rs : 500

*******MENU********
WELCOME TO BANK
1. Current Balance
2. Withdraw
3. Deposit
4. Exit
***************

ENTER YOUR CHOICE : 3

* * * * *ENTER THE DEPOSIT AMOUNT: 1000

* * * * *YOUR TOTAL BALANCE IS Rs : 1500

*******MENU********
WELCOME TO BANK
1. Current Balance
2. Withdraw
3. Deposit
4. Exit
***************

ENTER YOUR CHOICE : 4


thank you

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 8A: Demonstrate operator overloading two complex numbers.

using System;

namespace LabPgm8
{
public struct Complex
{
public int real;
public int imaginary;

public Complex(int real, int imaginary)


{
this.real = real;
this.imaginary = imaginary;
}

public static Complex operator +(Complex c1, Complex c2)


{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

public override string ToString()


{
return (String.Format("{0} + {1}i", real, imaginary));
}
}

class Program
{
static void Main()
{
int r1, i1, r2, i2;

Console.WriteLine("Enter real and imaginary part for 1st complex number:");


r1 = int.Parse(Console.ReadLine());

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

i1 = int.Parse(Console.ReadLine());

Console.WriteLine("\nEnter real and imaginary part for 2nd complex number:");


r2 = int.Parse(Console.ReadLine());
i2 = int.Parse(Console.ReadLine());

Complex num1 = new Complex(r1, i1);


Complex num2 = new Complex(r2, i2);

Complex sum = num1 + num2;

Console.WriteLine("\n\nFirst Complex Number : {0}", num1);


Console.WriteLine("Second Complex Number : {0}", num2);
Console.WriteLine("The Sum of the Two Numbers : {0}", sum);
Console.ReadLine();
}
}

OUTPUT

Enter real and imaginary part for 1st complex number:


2 3

Enter real and imaginary part for 2nd complex number:


4 5

First Complex Number : 2 + 3i


Second Complex Number : 4 + 5i
The Sum of the Two Numbers : 6 + 8i

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 9A: Demonstrate Dialog Box (Modal and Modeless)

FORM1:

using System;
using System.Windows.Forms;

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

private void Form1_Load(object sender, EventArgs e)


{

private void button1_Click_1(object sender, EventArgs e)


{
Form2 f = new Form2();
f.ShowDialog();
}

private void button2_Click_1(object sender, EventArgs e)


{
Form2 f = new Form2();
f.Show();
}
}
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

FORM2:

using System;
using System.Windows.Forms;

namespace LabPgm9
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

OUTPUT

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

LAB 10A: Write a C# program to create Menu and Menu items

using System;
using System.Drawing;
using System.Windows.Forms;

namespace LabPgm10
{
public partial class Form1 : Form
{
private MainMenu mainMenu;

public Form1()
{
InitializeComponent();

mainMenu = new MainMenu();

MenuItem File = mainMenu.MenuItems.Add("&File");

File.MenuItems.Add(new MenuItem("&New"));
File.MenuItems.Add(new MenuItem("&Open"));
File.MenuItems.Add(new MenuItem("&Exit"));

this.Menu = mainMenu;

MenuItem About = mainMenu.MenuItems.Add("&About");


About.MenuItems.Add(new MenuItem("&About"));

this.Menu = mainMenu;

mainMenu.GetForm().BackColor = Color.Indigo;
}
}
}

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:


USN No. _________________________ Subject: C# & Dot Net Framework Lab

OUTPUT

JAIN COLLEGE OF BBA, BCA AND COMMERCE, BELAGAVI Page:

You might also like