C# Lab BCA III Sem-2
C# Lab BCA III Sem-2
System, Version, Physical Memory and calculate the time since the Last Boot Up.(Hint: Use System.
Environment Class)
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;
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;
}
namespace Pgm04
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
#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;
}
}
}
#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
#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;
}
}
}