Lab
Lab
Lab
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!
/* actual initialization */
a = 10;
b = 20;
c = a + b;
Console.WriteLine ("a = {0}, b = {1}, c = {2}", a, b, c);
Console.ReadLine ();
}
}
}
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';
Short s = 56;
3
decimal dec = 389.5m;
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);
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
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());
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.
arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
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;
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.
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.
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-5: Double Click the view control Add the some code.
namespace cshorpfom
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
27
private void X_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
else gender = "Female";
string course="";
if (checkBox1.Checked == true) ;
{
course = "c# ";
}
if (checkBox2.Checked == true) ;
{
course = "vb ";
}
if (checkBox3.Checked == true) ;
{
course = "java ";
}
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.
Step-4: Then click the login control and add the code.
End If
End Sub
31
End Sub
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-6: Double click the Start timer and also next Click on the Stop Timer.
End Sub
End Sub
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.
End Sub
37
Label4.Text = "hobby is " & CheckBox3.Text
End If
End Sub
End If
Label4.Text = " hobby is " & CheckBox2.Text
End Sub
End Class
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-6: Double Click the Add button and add the code .
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 –
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