Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Part A 2024-25

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

PART-A C# AND DOT NET FRAMEWORK

LAB

1. Design a VB form to accept a number and display its


factorial. Input is to be accepted using Textbox and the
output can be displayed using TextBox/Label. Perform
validation for empty input, non numeric input, as well as
negative input and display error message, accordingly
using a message box.

SOURCE CODE:

Public Class Form1

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


e As System.EventArgs) Handles Label1.Click
Label1.Visible = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button1.Click
Dim a, i As Integer
Dim fact As Long = 1
If TextBox1.Text = "" Then
MessageBox.Show("Text box is empty", "Error",
MessageBoxButtons.OK)
ElseIf IsNumeric(TextBox1.Text) = False Then
MessageBox.Show("Please enter only a number", "Error",
MessageBoxButtons.OK)
Else
a = Val(TextBox1.Text)
If a < 0 Then
MessageBox.Show("Enter on1positive number", "Error",
MessageBoxButtons.OK)
Else
For i = 1 To a
fact = fact * i
Next
Label1.Visible = True
Label2.Text = "Factorial of " & a & " is: " &
fact.ToString()

1
PART-A C# AND DOT NET FRAMEWORK
LAB

End If
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox1.Focus()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub
End Class

OUTPUT:

2
PART-A C# AND DOT NET FRAMEWORK
LAB

2. Design the VB interface containing


a. A picture box whose picture should be changed every
5 second (use 5 pictures).
b. Textboxes to display date and time and day greeting
based on time. Time has to be changed every second
automatically.
c. Use scrollbars to change font size and background
color(RGB)of the textbox that shows greeting.
[use timer, scrollbars]

SOURCE CODE:

Public Class Form1

Private Sub HScrollBar1_Scroll(ByVal sender As System.Object,


ByVal e As System.Windows.Forms.ScrollEventArgs) Handles
HScrollBar1.Scroll
3
PART-A C# AND DOT NET FRAMEWORK
LAB

TextBox3.BackColor = Color.FromArgb(Rnd() * 225, 0, 0)


End Sub

Private Sub HScrollBar2_Scroll(ByVal sender As System.Object,


ByVal e As System.Windows.Forms.ScrollEventArgs) Handles
HScrollBar2.Scroll
TextBox3.BackColor = Color.FromArgb(Rnd() * 0, 225, 0)
End Sub

Private Sub HScrollBar3_Scroll(ByVal sender As System.Object,


ByVal e As System.Windows.Forms.ScrollEventArgs) Handles
HScrollBar3.Scroll
TextBox3.BackColor = Color.FromArgb(Rnd() * 0, 0, 225)
End Sub

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


e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = Today
TextBox2.Text = TimeOfDay
Timer1.Start()
Dim t As Integer
t = Date.Now.ToString("hh")
If (t >= 1 And t <= 12) Then
TextBox3.Text = "good morning"
ElseIf (t >= 12 And t <= 16) Then
TextBox3.Text = "good afternoon"
ElseIf (t >= 16 And t <= 20) Then
TextBox3.Text = "good evening"
Else
TextBox3.Text = "good night"
End If
End Sub

Private Sub VScrollBar1_Scroll(ByVal sender As System.Object,


ByVal e As System.Windows.Forms.ScrollEventArgs) Handles
VScrollBar1.Scroll
TextBox3.Font = New Font(TextBox3.Font.FontFamily,
VScrollBar1.Value)
4
PART-A C# AND DOT NET FRAMEWORK
LAB

End Sub

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


e As System.EventArgs) Handles Timer1.Tick
Static i As Integer
If i < ImageList1.Images.Count - 1 Then
i=i+1
Else
i=0
End If
PictureBox1.Image = ImageList1.Images(i)
End Sub
End Class

OUTPUT:

5
PART-A C# AND DOT NET FRAMEWORK
LAB

3. Design a VB interface to add, remove, search and clear


and items in a combo box. The item name to be added,
removed or searched can be accepted through input box.
Use a general procedure to find the existence of item
before deleting or while searching.

SOURCE CODE:

6
PART-A C# AND DOT NET FRAMEWORK
LAB

Public Class Form1

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender


As System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged

End Sub

Private Sub Button1_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button1.Click
Dim a As String
a = InputBox("Enter a String")
ComboBox1.Items.Add(a)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button2.Click
Dim item, ritem As String
Dim flag As Integer
ritem = InputBox("enter the items to be removed")
For Each item In ComboBox1.Items
If UCase(ritem) = UCase(item) Then
flag = 1
ComboBox1.Items.Remove(ritem)
Exit For
End If
Next
If flag = 0 Then
MsgBox("item not found on the list")
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button3.Click
Dim item, sitem As String
Dim flag As Integer
sitem = InputBox("enter the item to be searched")
7
PART-A C# AND DOT NET FRAMEWORK
LAB

For Each item In ComboBox1.Items


If UCase(sitem) = UCase(item) Then
flag = 1
Exit For
End If
Next
If flag = 1 Then
MsgBox("item is found in the list")
Else
MsgBox("item is not found in the list")
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button4.Click
ComboBox1.Items.Clear()
End Sub

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


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

OUTPUT:

8
PART-A C# AND DOT NET FRAMEWORK
LAB

4. Write a VB program find GCD and LCM of two numbers.


Accept input through textbox and Display the results in
label. Also validate for invalid input such as empty input,
non- numeric and negative integer.

9
PART-A C# AND DOT NET FRAMEWORK
LAB

SOURCE CODE:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button1.Click
If (TextBox1.Text = "" Or TextBox2.Text = "") Then
MsgBox("Enter Input")
ElseIf Not (IsNumeric(TextBox1.Text) And
IsNumeric(TextBox2.Text)) Then
MsgBox("Please enter Number")
Else
Dim a As Integer = (Int(TextBox1.Text))
Dim b As Integer = (Int(TextBox2.Text))
If (a < 0 Or b < 0) Then
MsgBox("Enter Positive")
Else
Label6.Text = gcd(a, b)
Label7.Text = Lcm(a, b)
End If
End If
End Sub
Public Function Lcm(ByVal a As Integer, ByVal b As Integer) As
Integer
If a = 0 OrElse b = 0 Then
Return 0
End If
Return (Int((a * b) / gcd(a, b)))

10
PART-A C# AND DOT NET FRAMEWORK
LAB

End Function
Public Function gcd(ByVal a As Integer, ByVal b As Integer) As
Integer
If a < 1 Or b < 1 Then
Throw New ArgumentException("a or b is less than 1")
End If
Dim large As Integer = Math.Max(a, b)
Dim small As Integer = Math.Min(a, b)
Dim r As Integer = 0
Do
r = large Mod small
large = small
small = r
Loop While small <> 0
Return large
End Function
End Class

OUTPUT:

11
PART-A C# AND DOT NET FRAMEWORK
LAB

5. Write a program in C# to check a number if it is prime;


otherwise display the factor of that number.

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

namespace PartA5
{
class Program
{
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)
{
if (flag == 0)
{
Console.WriteLine("Number is not Prime:");
Console.WriteLine("The factors are:");
}
Console.WriteLine(i);
flag = 1;
}
}
if (flag == 0)
Console.Write("Number is Prime");
Console.ReadLine();

}
}

12
PART-A C# AND DOT NET FRAMEWORK
LAB

OUTPUT:

13
PART-A C# AND DOT NET FRAMEWORK
LAB

6. Write a program in c# define a class “salary” which will


contain member variable Emp_no, Emp_name, DOB basic.
Write a program using constructor and method to
calculate the DA, HRA, PF, IT Gross and Netpay using
appropriate condition.
If basic<=20000 DA is 40% Basic HRA is 10% Basic.
PF 12% of gross:PT is RS.100
If basic>20000 DA is 50% Basic HRA is 15% Basic.
PF 12% of gross:PT is RS.150
Gross=Basic+DA+HRA and Net=Gross-PT-PF.

using System;
namespace PartA6
{
class Salary
{
int Empno;
String empname, DOB;
double basic;
public Salary(int eno, string name, string db, double sal)
{
Empno = eno;
empname = name;
DOB = db;
basic = sal;
}
public void display()
{
Console.WriteLine("Empno:"+Empno);
Console.WriteLine("Empname:"+empname);
Console.WriteLine("DOB:"+DOB);
Console.WriteLine("Basic salary:"+basic);
}
public void calculate()
{
14
PART-A C# AND DOT NET FRAMEWORK
LAB

int pt;
double da,hra,pf,gross,net;
if(basic<=20000)
{
da=0.4*basic;
hra=0.10*basic;
gross=basic+da+hra;
pf=0.12*gross;
pt=100;
net=gross-pt-pf;
}
else
{
da=0.5*basic;
hra=0.15*basic;
gross=basic+da+hra;
pf=0.12*gross;
pt=150;
net=gross-pt-pf;
}
Console.WriteLine("DA:"+da);
Console.WriteLine("HRA:"+hra);
Console.WriteLine("pf:"+pf);
Console.WriteLine("Professional Tax:"+pt);
Console.WriteLine("Net:"+net);
Console.WriteLine("Gross:"+gross);
}
static void Main(string[] args)
{
Salary S;
S=new Salary(1,"Akash","21-12-1996",20000.50);
S.display();
S.calculate();
Console.ReadLine();
}
}

15
PART-A C# AND DOT NET FRAMEWORK
LAB

OUTPUT:

16
PART-A C# AND DOT NET FRAMEWORK
LAB

7. Write a program in C# to find addition and


multiplication operation on two complex number using
operator overloading.

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

namespace complex
{
class complex
{
public int real;
public int img;
public complex()
{
real=0;
img=0;
}
public complex(int real,int img)
{
this.real=real;
this.img=img;
}
public static complex operator+(complex ob1,complex ob2)
{
complex temp=new complex();
temp.real=ob1.real+ob2.real;
temp.img=ob1.img+ob2.img;
return temp;
17
PART-A C# AND DOT NET FRAMEWORK
LAB

}
public static complex operator*(complex ob1,complex ob2)
{
complex temp=new complex();
temp.real=(ob1.real*ob2.real)-(ob1.img*ob2.img);
temp.img=(ob1.real*ob2.img)+(ob1.img*ob2.real);
return temp;
}
public void printcomplexNumber()
{
Console.WriteLine("{0}+{1}i", real, img);
}
}
class program
{
static void Main(string[] args)
{
complex c1 = new complex(4, 2);
complex c2 = new complex(2, 3);
complex c3, c4;
c3 = c1 + c2;
c4 = c1 * c2;
Console.WriteLine("c1:");
c1.printcomplexNumber();
Console.Write("c2:");
c2.printcomplexNumber();
Console.WriteLine("The addition of two complex number
is");
Console.Write("c3:");
c3.printcomplexNumber();
Console.WriteLine("The multiplication of two complex
number is");
Console.Write("c4:");
c4.printcomplexNumber();
Console.ReadLine();

}
}
18
PART-A C# AND DOT NET FRAMEWORK
LAB

OUTPUT:

19

You might also like