0% found this document useful (0 votes)
579 views13 pages

C# Lab BCA III Sem-2

The document contains code for multiple C# programs that demonstrate different concepts like machine details, string analysis, digit sum calculation, shapes drawing, inheritance, exceptions handling and bank transactions. The programs contain classes and methods to perform various operations.

Uploaded by

jobajo5825
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)
579 views13 pages

C# Lab BCA III Sem-2

The document contains code for multiple C# programs that demonstrate different concepts like machine details, string analysis, digit sum calculation, shapes drawing, inheritance, exceptions handling and bank transactions. The programs contain classes and methods to perform various operations.

Uploaded by

jobajo5825
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/ 13

Lab Program No:01 Write a C# program to show the machine details like machine name, Operating

System, Version, Physical Memory and calculate the time since the Last Boot Up.(Hint: Use System.
Environment Class)

/* Give project name as Lab1*/


using System;
namespace Lab1
{
class Program
{
static void Main(string[] args)
{
int tickCount = 0;

tickCount = Environment.TickCount;
Console.WriteLine("The time since the Last Boot Up: " + tickCount);
Console.WriteLine("OS Version is= " + Environment.OSVersion.Version.ToString()+ "\n");
Console.WriteLine("Machinename is= " + Environment.MachineName);
Console.WriteLine("OS Platoform: " + Environment.OSVersion.Platform.ToString());
foreach (String s3 in Environment.GetLogicalDrives())
Console.WriteLine("Drive: " + s3 + "\n");
Console.ReadLine();
}

}
}

#output
The time since the Last Boot Up: 1129433343
OS Version is= 6.2.9200.0
Machinename is= LAPTOP-D25H1M72
OS Platoform: Win32NT
Drive: C:\
Drive: D:\
Lab Program No:02 Write a program in C# Sharp to count a total number of alphabets, digits
and special characters in a string
/* Give project name as Lab2*/
using System;
namespace Lab2
{
class Program
{
static void Main(string[] args)
{
string str;
int alp, digit, splch, i, l;
alp = digit = splch = i = 0;
Console.WriteLine("Count total number of alphabets, digits & special characters
:\n");
Console.WriteLine("--------------------------------------------------\n");
Console.WriteLine("Enter a string : ");
str = Console.ReadLine();
l = str.Length;

/* Checks each character of string*/

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++;
}

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

#Output
Count total number of alphabets, digits and special characters :
--------------------------------------------------------------------
Enter a string : i am 30 year old and my roll number is U22S09876.....!
Number of Alphabets in the string is : 29
Number of Digits in the string is : 9
Number of Special characters in the string is : 16
Lab Program: 03: Write a C# program to create a function to calculate the sum of the
individual digits of a given number.
/* Give project name as Lab3*/
using System;

namespace Lab3
{
class Program
{
public static int SumCal(int number)
{
string n1 = Convert.ToString(number);
int sum = 0;
for (int i = 0; i < n1.Length; i++)
sum += Convert.ToInt32(n1.Substring(i, 1));
return sum;
}

public static void Main()


{
int num;
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));
Console.ReadLine();
}
}
}
#output
Enter a number: 756644
The sum of the digits of the number 756644 is : 32
Lab Program No:04 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.
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 Pgm04
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void panel1_Paint(object sender, PaintEventArgs e)


{
Graphics g = panel1.CreateGraphics();
Pen pblack = new Pen(Color.Black);
int x = (this.ClientSize.Width / 2) - 50;
int y = (this.ClientSize.Height / 2) - 50;
int h = 100;
int w = 100;
g.DrawRectangle(pblack,x,y,h,w);
Pen pred = new Pen(Color.Red);
g.DrawEllipse(pred, x, y, h, w);
}

}
}

#output
Lab Program:05 Write a program to implement multilevel inheritance
using System;
namespace Lab5
{
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);
}
}
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();
}
}
}
#output
Enter Mobile details
Enter Brand Name:
Samsung
Enter Model:
Galaxy Note
Enter Mobile Price
28000
Enter Discount amount
400
........................
Mobile details are:
Mobile Name:Samsung
Mobile Model=Galaxy Note
Total Amount=27600
Lab Program No:06 program to demonstrate system exception
using System;
namespace SysExc
{
class Program
{
static void Main(string[] args)
{
int Number1, Number2, Result;
try
{
Console.WriteLine("Enter First Number");
Number1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second Number");
Number2 = int.Parse(Console.ReadLine());
Result = Number1 / Number2;
Console.WriteLine("Result:”, + Result);
}

catch (DivideByZeroException)
{
Console.WriteLine("Second Number Should Not Be Zero");
}
catch (FormatException fe)
{
Console.WriteLine("Enter Only Integer Numbers");
}
catch (Exception)
{
Console.WriteLine("Generic Catch Block...");
}
Console.ReadKey();
}
}
}
#OUTPUT:1
Enter First Number
100
Enter Second Number
50
Result: 2

#Output :2
Enter First Number
100
Enter Second Number
0
Second Number Should Not Be Zero

#OUTPUT:3
Enter First Number
100
Enter Second Number
abcd
Enter Only Integer Numbers
Lab Program No:07 Write a object oriented program to demonstrate bank
transaction. Include methods for amount deposit, withdraw and display
/* Give project name as Lab7*/
using System;

namespace Lab7
{
public class BankDetails
{
int amount = 1000, dep_amt, draw_amt;

public void CheckBal()


{
Console.WriteLine("\n YOUR BALANCE IN Rs : {0} ", amount);
}
public void Withdraw()
{
Console.WriteLine("\n ENTER THE AMOUNT TO WITHDRAW: ");
draw_amt = int.Parse(Console.ReadLine());
if (draw_amt % 100 != 0)
{
Console.WriteLine("\n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100");
}
else if (draw_amt > (amount - 500))
{
Console.WriteLine("\n INSUFFICENT BALANCE");
}
else
{
amount = amount - draw_amt;
Console.WriteLine("\n\n PLEASE COLLECT CASH");
Console.WriteLine("\n YOUR CURRENT BALANCE IS {0}", amount);
}
}
public void Deposit()
{
Console.WriteLine("\n ENTER THE AMOUNT TO DEPOSIT");
dep_amt = int.Parse(Console.ReadLine());
amount = amount + dep_amt;
Console.WriteLine("YOUR BALANCE IS {0}", amount);
}
}
class Program
{
static void Main(string[] args)
{
int choice, pin = 0;
Console.WriteLine("Enter Your Pin Number ");
pin = int.Parse(Console.ReadLine());
BankDetails b = new BankDetails();
while (true)
{
Console.WriteLine("********Welcome to ATM Service**************\n");
Console.WriteLine("1. Check Balance\n");
Console.WriteLine("2. Withdraw Cash\n");
Console.WriteLine("3. Deposit Cash\n");
Console.WriteLine("4. Quit\n");
Console.WriteLine("********************************************\n");
Console.WriteLine("Enter your choice: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
b.CheckBal();
break;
case 2:
b.Withdraw();
break;
case 3:
b.Deposit();
break;
case 4:
Console.WriteLine("\n THANK YOU FOR USING ATM");
break;
}
Console.WriteLine("\n THANKS FOR USING OUR ATM SERVICE");
}

}
}
}

#Output
Enter Your Pin Number
2345
********Welcome to ATM Service**************
1. Check Balance
2. Withdraw Cash
3. Deposit Cash
4. Quit
*********************************************
Enter your choice:
1
YOUR BALANCE IN Rs : 1000
THANKS FOR USING OUT ATM SERVICE
********Welcome to ATM Service**************
1. Check Balance
2. Withdraw Cash
3. Deposit Cash
4. Quit
*********************************************
Enter your choice:
2
ENTER THE AMOUNT TO WITHDRAW:
700
INSUFFICENT BALANCE
THANKS FOR USING OUT ATM SERVICE
********Welcome to ATM Service**************
1. Check Balance
2. Withdraw Cash
3. Deposit Cash
4. Quit
*********************************************
Enter your choice:
3
ENTER THE AMOUNT TO DEPOSIT
2000
YOUR BALANCE IS 3000
THANKS FOR USING OUT ATM SERVICE
********Welcome to ATM Service**************
1. Check Balance
2. Withdraw Cash
3. Deposit Cash
4. Quit
Lab Program No:08 Demonstrate operator overloading two complex numbers
using System;
public class Complex
{
public int real;
public int imag;
public Complex(int real, int imag)
{
this.real = real;
this.imag = imag;
}
// Syntax of Operator Overloading
public static Complex operator +(Complex n1, Complex n2)
{
return new Complex(n1.real + n2.real, n1.imag + n2.imag);
}
// Override the ToString method to display an complex number in the
suitable format:
public override string ToString()
{
return (String.Format("{0} + {1}i", real, imag));
}
public static void Main()
{
Complex cnum1 = new Complex(4, 5);
Complex cnum2 = new Complex(5, 6);
// Add two Complex objects (num1 and num2) through the
Complex add = cnum1 + cnum2;
// Print the numbers
Console.WriteLine("First complex number: {0}", cnum1);
Console.WriteLine("Second complex number: {0}", cnum2);
Console.WriteLine("The sum of the two numbers: {0}", add);
// Hault the output screen
Console.Read();
}
}

#output

First complex number: 4 + 5i


Second complex number: 5 + 6i
The sum of the two numbers: 9 + 11i
Lab Program No:09 Demonstrate Dialog box (Modal and Modeless)
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 Pgm9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


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

private void button2_Click(object sender, EventArgs e)


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

#output
Lab Program No:10 Write a program in C# Sharp to create Menu and menu items.
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 MenuTest
{
public partial class MenuTest1 : Form
{
private MainMenu m;

public Form1()
{
InitializeComponent();
m = new MainMenu();
MenuItem File = m.MenuItems.Add("&File");
File.MenuItems.Add(new MenuItem("&New"));
File.MenuItems.Add(new MenuItem("&Open"));
File.MenuItems.Add(new MenuItem("&Exit"));
MenuItem About = m.MenuItems.Add("&About");
About.MenuItems.Add(new MenuItem("&About"));
this.Menu = m;
mainMenu.GetForm().BackColor = Color.Indigo;
}
}
}

You might also like