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

Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

C#.

NET

1
1. (A) Write a Simple program on c#.

Using System;
Public class Program
{
Public static void Main (string [] args)
{
Console.WriteLine ("Hello World!");
Console.Readline ( );
}
}

Output:

Hello World!

1. (B) Write a program on how to initialize, access the variable in c#


Using System;

Namespace Variable Definition {


Class Program {
Static void Main (string [] args) {
Short a;
Int b;
Double c;

/* actual initialization */
a = 10;
b = 20;
c = a + b;
Console.WriteLine ("a = {0}, b = {1}, c = {2}", a, b, c);
Console.ReadLine ();
}
}
}

Output: a = 10, b = 20, c = 30

2
2. Write a program various data types in c#

Using System;
Namespace ValueTypeTest {

Class Program {

// Main function
Static void Main ()
{

// declaring character
Char a = 'G';

// Integer data type is generally


// used for numeric values
Int i = 89;

Short s = 56;

// long uses Integer values which


// may signed or unsigned
Long l = 4564;

// UInt data type is generally


// used for unsigned integer values
uint ui = 95;
ushort us = 76;

// ulong data type is generally


// used for unsigned integer values
ulong ul = 3624573;

// by default fraction value


// is double in C#
double d = 8.358674532;

// for float use 'f' as suffix


float f = 3.7330645f;
// for float use 'm' as suffix

3
decimal dec = 389.5m;

Console.WriteLine ("char: " + a);


Console.WriteLine ("integer: " + i);
Console.WriteLine ("short: " + s);
Console.WriteLine ("long: " + l);
Console.WriteLine ("float: " + f);
Console.WriteLine ("double: " + d);
Console.WriteLine ("decimal: " + dec);
Console.WriteLine ("Unsinged integer: " + ui);
Console.WriteLine ("Unsinged short: " + us);
Console.WriteLine ("Unsinged long: " + ul);
}
}
}

Output:
char: G
integer: 89
short: 56
long: 4564
float: 3.733064
double: 8.358674532
decimal: 389.5
Unsinged integer: 95
Unsinged short: 76
Unsinged long: 3624573

4
3. (A). Write a program Relational operators in c#
Using System;
namespace Relational {

class Program {

// Main Function
Static void Main (string [] args)
{
bool result;
int x = 5, y = 10;

// Equal to Operator
result = (x == y);
Console.WriteLine ("Equal to Operator: " + result);

// Greater than Operator


result = (x > y);
Console.WriteLine ("Greater than Operator: " + result);

// Less than Operator


result = (x < y);
Console.WriteLine("Less than Operator: " + result);

// Greater than Equal to Operator


result = (x >= y);
Console.WriteLine ("Greater than or Equal to: "+ result);

// Less than Equal to Operator


result = (x <= y);
Console.WriteLine ("Lesser than or Equal to: "+ result);

// Not Equal To Operator


result = (x != y);
Console.WriteLine ("Not Equal to Operator: " + result);
}
}
}

5
Output:
Equal to Operator: False
Greater than Operator: False
Less than Operator: True
Greater than or Equal to: False
Lesser than or Equal to: True
Not Equal to Operator: True

3. (B). Write a program Logical operators in c#


using System;
namespace Logical {

class Program {

// Main Function
static void Main(string[] args)
{
bool a = true,b = false, result;

// AND operator
result = a && b;
Console.WriteLine("AND Operator: " + result);

// OR operator
result = a || b;
Console.WriteLine("OR Operator: " + result);
// NOT operator
result = !a;
Console.WriteLine("NOT Operator: " + result);
} }
}
Output:
AND Operator: False
OR Operator: True
NOT Operator: False
6
4. (A). Write a program Number is Even or odd in c#.

using System;
public class IfExample
{
public static void Main(string[] args)
{
int num = 10;
if (num % 2 == 0)
{
Console.WriteLine("It is even number");
}

}
}
Output:
It is even number
4. (B). Write a program Find out the grades using if else-if in c#.

using System;
public class program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number to check grade:");
int num = Convert.ToInt32(Console.ReadLine());

if (num <0 || num >100)


{
Console.WriteLine("wrong number");
}
else if(num >= 0 && num < 50){
Console.WriteLine("Fail");
}
else if (num >= 50 && num < 60)

7
{
Console.WriteLine("D Grade");
}
else if (num >= 60 && num < 70)
{
Console.WriteLine("C Grade");
}
else if (num >= 70 && num < 80)
{
Console.WriteLine("B Grade");
}
else if (num >= 80 && num < 90)
{
Console.WriteLine("A Grade");
}
else if (num >= 90 && num <= 100)
{
Console.WriteLine("A+ Grade");
}
}
}

Output:
Enter a number to check grade:66
C Grade
Output:
Enter a number to check grade:-2
wrong number

8
5. Write a c# program Find out the number using in switch case.

using System;
public class SwitchExample
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int num = Convert.ToInt32(Console.ReadLine());

switch (num)
{
case 10: Console.WriteLine("It is 10"); break;
case 20: Console.WriteLine("It is 20"); break;
case 30: Console.WriteLine("It is 30"); break;
default: Console.WriteLine("Not 10, 20 or 30"); break;
}
}
}

Output:
Enter a number:
10
It is 10

Output:
Enter a number:
55
Not 10, 20 or 30

9
6. Write a c# program Single dimensional array.

using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = new int[5];//creating array
arr[0] = 10;//initializing array
arr[2] = 20;
arr[4] = 30;

//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}

Output:
10
0
20
0
30

10
7. Write a c# program Multi-dimensional array.

using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;

//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}

Output:
0 10 0
0 0 20
30 0 0

11
8. Write a c# program Jagged array.

public class JaggedArrayTest


{
public static void Main()
{
int[][] arr = new int[2][];// Declare the array

arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };

// Traverse array elements


for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write(arr[i][j]+" ");
}
System.Console.WriteLine();
}
}
}

Output:
11 21 56 78
42 61 37 41 59 63
12
9. Write a c# program using objects and class Display data through
method.

sing System;
public class Student
{
public int id;
public String name;
public void insert(int i, String n)
{
id = i;
name = n;
}
public void display()
{
Console.WriteLine(id + " " + name);
}
}
class TestStudent{
public static void Main(string[] args)
{
Student s1 = new Student();
Student s2 = new Student();
s1.insert(101, "Ajeet");
s2.insert(102, "Tom");
s1.display();
s2.display();

}
}

Output:
101 Ajeet
102 Tom
13
10. Write a c# program using constructors.

using System;
public class Employee
{
public int id;
public String name;
public float salary;
public Employee(int i, String n,float s)
{
id = i;
name = n;
salary = s;
}
public void display()
{
Console.WriteLine(id + " " + name+" "+salary);
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee(101, "Sonoo", 890000f);
Employee e2 = new Employee(102, "Mahesh", 490000f);
e1.display();
e2.display();

}
}

Output:
101 Sonoo 890000
102 Mahesh 490000

14
11. Write a c# program using Inheritance.

using System;
public class Animal
{
public void eat() { Console.WriteLine("Eating..."); }
}
public class Dog: Animal
{
public void bark() { Console.WriteLine("Barking..."); }
}
public class BabyDog : Dog
{
public void weep() { Console.WriteLine("Weeping..."); }
}
class TestInheritance2{
public static void Main(string[] args)
{
BabyDog d1 = new BabyDog();
d1.eat();
d1.bark();
d1.weep();
}
}

Output:
Eating...
Barking...
Weeping...

15
12. Write a c# program using Method Overriding.

using System;
public class Animal{
public virtual void eat(){
Console.WriteLine("Eating...");
}
}
public class Dog: Animal
{
public override void eat()
{
Console.WriteLine("Eating bread...");
}
}
public class TestOverriding
{
public static void Main()
{
Dog d = new Dog();
d.eat();
}
}

Output:
Eating bread...

16
13. Write a c# program using Method Over loading.

using System;
public class Cal{
public static int add(int a,int b){
return a + b;
}
public static int add(int a, int b, int c)
{
return a + b + c;
}
}
public class TestMemberOverloading
{
public static void Main()
{
Console.WriteLine(Cal.add(12, 23));
Console.WriteLine(Cal.add(12, 23, 25));
}
}

Output:
35
60

17
14. Write a c# program using Abstract class and Abstract Method.

using System;
public abstract class Shape
{
public abstract void draw();
}
public class Rectangle : Shape
{
public override void draw()
{
Console.WriteLine("drawing rectangle...");
}
}
public class Circle : Shape
{
public override void draw()
{
Console.WriteLine("drawing circle...");
} }
public class TestAbstract
{
public static void Main()
{
Shape s;
s = new Rectangle();
s.draw();
s = new Circle();
s.draw();
} }

Output:
drawing ractangle...
drawing circle...

18
15. Write a c# program using Inter Faces.

using System;
public interface Drawable
{
void draw();
}
public class Rectangle : Drawable
{
public void draw()
{
Console.WriteLine("drawing rectangle...");
}
}
public class Circle : Drawable
{
public void draw()
{
Console.WriteLine("drawing circle...");
} }
public class TestInterface
{
public static void Main()
{
Drawable d;
d = new Rectangle();
d.draw();
d = new Circle();
d.draw();
} }

Output:
drawing ractangle...
drawing circle...
19
16. Write a c# program sum of N natural number.
using System;

class MyProgram {
static void Main(string[] args) {
int n = 10;
int i = 1;
int sum = 0;

//calculating sum from 1 to n


while(i <= n) {
sum += i;
i++;
}

Console.WriteLine("Sum is: " + sum);


}
}

Output:
Sum is: 55

20
17. Write a c# program Find out Fibonacci number.

using System;
public class FibonacciExample
{
public static void Main(string[] args)
{
int n1=0,n2=1,n3,i,number;
Console.Write("Enter the number of elements: ");
number = int.Parse(Console.ReadLine());
Console.Write(n1+" "+n2+" "); //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are alr
eady printed
{
n3=n1+n2;
Console.Write(n3+" ");
n1=n2;
n2=n3;
}
}
}

Output:
Enter the number of elements: 15
0 1 1 2 3 5 8 13 21 34 55 89 144 233
377

21
18. Write a c# program Find out Factorial Number.

using System;
public class FactorialExample
{
public static void Main(string[] args)
{
int i,fact=1,number;
Console.Write("Enter any Number: ");
number= int.Parse(Console.ReadLine());
for(i=1;i<=number;i++){
fact=fact*i;
}
Console.Write("Factorial of " +number+" is: "+fact);
}
}

Output:
Enter any Number: 6
Factorial of 6 is: 720

22
19. Write a c# program Find out Armstrong Number or not.

using System;
public class ArmstrongExample
{
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+(r*r*r);
n=n/10;
}
if(temp==sum)
Console.WriteLine("Armstrong Number.");
else
Console.WriteLine("Not Armstrong Number.");
}
}

Output:
Enter the Number= 371
Armstrong Number.
Enter the Number= 342
Not Armstrong Number.

23
20. Write a c# program palindrome Number or not.

using System;
public class PalindromeExample
{
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");
}
}

Output:
Enter the Number=121
Number is Palindrome.

Enter the number=113


Number is not Palindrome.

24
21. Write a c# program Exception handling Try, catch, finally keywords.

using System;
public class ExExample
{
public static void Main(string[] args)
{
try
{
int a = 10;
int b = 0;
int x = a / b;
}
catch (Exception e) { Console.WriteLine(e); }
finally { Console.WriteLine("Finally block is executed"); }
Console.WriteLine("Rest of the code");
}
}

Output:
System.DivideByZeroException:
Attempted to divide by zero.
Finally block is executed
Rest of the code

25
22. Write a c# program palindrome Number or not.
using System;
public class PrimeNumberExample
{
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.");
}
}

Output:
Enter the Number to check Prime: 17
Number is Prime.

Enter the Number to check Prime: 57


Number is not Prime.

26
23. How to design the C# windows form application Display the student
Information.

Steo1: open the form and add the controls in the form.

Step-2: The controls are one text box and one button control, one combobox, two
radio buttons, three checkboxes, six labels.

Step-3: Set the Properties of all controls.

Step-4: Design the Form with All controls.

Step-5: Double Click the view control Add the some code.

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

private void label1_Click(object sender, EventArgs e)


{

27
private void X_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void button1_Click(object sender, EventArgs e)


{
String gender;
if (radioButton1.Checked == true)
{
gender = "Male";

}
else gender = "Female";
string course="";
if (checkBox1.Checked == true) ;
{
course = "c# ";
}
if (checkBox2.Checked == true) ;
{
course = "vb ";
}
if (checkBox3.Checked == true) ;
{
course = "java ";
}

MessageBox.Show("Name = " + textBox1.Text+ "Department"


+ comboBox1.Text + " gendr " + gender + "course = " + course);
}
}
}

28
Step-6: Run the Application Form.

OUTPUT:

29
VB.NET

30
24. How to design the Login windows form application.

Steo1: open the form and add the controls in the form

Step-2: The controls of the form three label controls, two text box controls and two
button controls.

Step3: Set the all Control properties.

Step-4: Then click the login control and add the code.

Private Sub Bton_Click(sender As Object, e As EventArgs) Handles


Bton.Click
If txtusername.Text = "abcd" And txtpassword.Text = "123" Then
MsgBox("you login sucess")
Else
MsgBox("wrong user name and password")

End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles


btonclose.Click
Me.Close()

31
End Sub

Step-5: Run the application

Step-6: Enter the Login details

32
OUTPUT:

33
25. How to Design the form using the Timer control.

Steo1: open the form and add the controls in the form.

Step-2: The controls are one text box and two button controls.

Step-3: The add the form on Timer control also.

Step-4: Set the Properties of all controls.

Step-5: Design the Form with All controls.

Step-6: Double click the Start timer and also next Click on the Stop Timer.

Add the coding of the Form.

Public Class Form1


Private Sub TextBox2_TextChanged(sender As Object, e As
EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles


MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles


Button1.Click
34
Timer1.Start()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles


Button2.Click
Timer1.Stop()
End Sub

Private Sub Timer1_Tick (sender As Object, e As EventArgs) Handles


Timer1.Tick
TextBox1.Text = TextBox1.Text + 1
End Sub
End Class

Step-7: Run the application code.

35
OUTPUT:

36
26. How to Design the form using the Group Box control, Radio Button control
and Checkbox control..

Steo1: open the form and add the controls in the form.

Step-2: The controls Four label controls, Two group box controls and three radio
button controls, and three check box controls.

Step-3: Design the Form with All controls.

Step-4: Set the Properties of all controls.

Step-5: Add the code all Controls.

Public Class Form1


Private Sub Label1_Click(sender As Object, e As EventArgs) Handles
Label1.Click

End Sub

Private Sub CheckBox3_CheckedChanged(sender As Object, e As


EventArgs) Handles CheckBox3.CheckedChanged
If CheckBox3.Checked = True Then

37
Label4.Text = "hobby is " & CheckBox3.Text
End If
End Sub

Private Sub RadioButton1_CheckedChanged(sender As Object, e As


EventArgs) Handles RadioButton1.CheckedChanged
Label3.Text = " Nationality Is " & RadioButton1.Text
End Sub

Private Sub RadioButton2_CheckedChanged(sender As Object, e As


EventArgs) Handles RadioButton2.CheckedChanged
Label3.Text = " Nationality Is " & RadioButton2.Text
End Sub

Private Sub RadioButton3_CheckedChanged(sender As Object, e As


EventArgs) Handles RadioButton3.CheckedChanged
Label3.Text = " Nationality Is " & RadioButton3.Text
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As


EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
Label4.Text = " hobby is " & CheckBox1.Text
End If
End Sub

Private Sub CheckBox2_CheckedChanged(sender As Object, e As


EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked = True Then

End If
Label4.Text = " hobby is " & CheckBox2.Text
End Sub
End Class

Step-6: Run the form application.

38
Output:

39
27. How to Design the form using the List Box control.

Steo1: open the form and add the controls in the form.

Step-2: The controls one text control, One List Box control and one button control.

Step-3: Design the Form with All controls.

Step-4: All Controls set the properties Design phase

Step-6: Double Click the Add button and add the code .

public Class Form1


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
ListBox1.Items.Add(TextBox1.Text)
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As


EventArgs) Handles ListBox1.SelectedIndexChanged
MessageBox.Show(ListBox1.SelectedItem.ToString)
End Sub
End Class

Step-7: Run The form application.

40
OUTPUT.

41
28. How to Design Menus Using Menu Strip Control.

The Menu Strip control represents the container for the menu structure.
The Menu Strip control works as the top-level container for the menu structure.
The ToolStripMenuItem class and the ToolStripDropDownMenu class provide the
functionalities to create menu items, sub menus and drop-down menus.
The following diagram shows adding a Menu Strip control on the form –

Take the following steps −


Step-1: Drag and drop or double click on a Menu Strip control, to add it to the
form.
Step-2: Click the Type Here text to open a text box and enter the names of the
menu items or sub-menu items you want. When you add a sub-menu, another
text box with 'Type Here' text opens below it.
Step-3: Complete the menu structure shown in the diagram above.

42
Step-4: Add a sub menu Exit under the File menu.

Step-5:Double-Click the Exit menu created and add the following code to the
Click event of ExitToolStripMenuItem −
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) _
Handles ExitToolStripMenuItem.Click
End
End Sub

Step-6: When the above code is executed and run using Start button available at the
Microsoft Visual Studio tool bar, it will show the following window:

43
Step-7: Click on the File -> Exit to exit from the application −

_________________________________________________________________________________________

44

You might also like