Documente Academic
Documente Profesional
Documente Cultură
Roll
Number: 0829cs071112
// 1. WAP to print “HELLO C#”
using System;
using System.Collections.Generic;
using System.Text;
namespace printname
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("HELLO C#");
Console.ReadLine();
}
}
}
1
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
2
Name: Tanmay Baid
Roll
Number: 0829cs071112
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the first number:");
string s1 = Console.ReadLine();
int a1 = Convert.ToInt32(s1);
Console.WriteLine("Enter the second number:");
string s2 = Console.ReadLine();
int a2 = Convert.ToInt32(s2);
int s3 = a1 + a2;
Console.WriteLine("The sum is:" + s3);
Console.ReadLine();
}
}
}
3
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
4
Name: Tanmay Baid
Roll
Number: 0829cs071112
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication
{
class Array
{
static void Main(string[] args)
{
int[] a = new int[5];
GetArray(a);
Console.WriteLine("ENTERED NUMBER IS\n");
for (int i = 0; i < a.Length; i++)
Console.WriteLine("a[" + i + "]=" + a[i]);
Console.ReadLine();
}
static void GetArray(int[] a)
{
Console.WriteLine("ENTER 5 NUMBERS");
for (int i = 0; i < a.Length; i++)
a[i] = int.Parse(Console.ReadLine());
}
}
}
5
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
6
Name: Tanmay Baid
Roll
Number: 0829cs071112
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication6
{
class TwoDArray
{
static void Main(string[] args)
{
int[,] a;
a = new int[2, 3];
a[0, 0] = 50;
a[0, 1] = 60;
a[0, 2] = 70;
a[1, 0] = 80;
a[1, 1] = 90;
a[1, 2] = 100;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine("press any key to exit");
Console.ReadLine();
}
}
}
7
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
8
Name: Tanmay Baid
Roll
Number: 0829cs071112
/* 5. WAP to print jagged array and also find the number of rows and number
of columns in each row.*/
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
{
class Demo
{
static void Main(string[] args)
{
int[][] a = new int[2][];
a[0] = new int[] { 89,94,46,54,64 };
a[1] = new int[] {12,56};
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < a[i].Length;j++)
Console.Write(" " + a[i][j]);
Console.WriteLine();
}
Console.WriteLine("no of rows=" +a.Length);
Console.WriteLine("no of column in first row=" + a[0].Length);
Console.WriteLine("no of column in second row=" + a[1].Length);
Console.WriteLine("\npress any key to exit");
Console.ReadLine();
}
}
}
9
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
10
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 6. WAP to find sum of two number using function.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication8
{
class Sumof
{
static void Main(string[] args)
{
int a, b;
Console.Write("Enter 1st Number ");
a = int.Parse(Console.ReadLine());
Console.Write("Enter 2nd Number ");
b = int.Parse(Console.ReadLine());
int c = sum(a, b);
Console.WriteLine("Sum of " + a + " & " + b + " is =" + c);
Console.ReadLine();
}
static int sum(int a, int b)
{
int c = a + b;
return c;
}
}
}
11
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
12
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 7. WAP to show how static data member works in a class.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication9
{
class A
{
static int n;
public void Get(int x)
{
n = x;
}
public void Show()
{
Console.WriteLine("n=" + n);
}
}
class sta_tic
{
static void Main()
{
A a = new A();
a.Get(99);
a.Show();
A b = new A();
b.Get(200);
b.Show();
a.Show();
Console.ReadLine();
}
}
}
13
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
14
Name: Tanmay Baid
Roll
Number: 0829cs071112
/*8. WAP to check whether the two entered string are equal or not using
string class and its methods. also find the length of each string.*/
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication10
{
class Demo
{
public static void Main(string[] args)
{
string s = "Check";
string t = "Check";
if (s.Equals(t))
Console.WriteLine("Strings are Same");
else
Console.WriteLine("String are Different");
Console.WriteLine("Length of the string s is= " + s.Length);
Console.ReadLine();
}
}
}
15
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
16
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 9. WAP to show how we can use structure in a class.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication11
{
struct Student
{
int rollno;
string name;
public void SetData(int r, string n)
{
rollno = r;
name = n;
}
public void ShowData()
{
Console.WriteLine("ROLL NO=:" + rollno);
Console.WriteLine("Name=:" + name);
}
}
class struc
{
static void Main(string[] args)
{
Student s = new Student();
s.SetData(52, "Tanmay Baid");
s.ShowData();
Console.ReadLine();
}
}
}
17
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
18
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 10. WAP to show how structure variable works differently than class
variable (object).
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication12
{
struct Student
{
int rollno;
string name;
public void SetData(int r, string n)
{
rollno = r;
name = n;
}
public void ShowData()
{
Console.WriteLine(rollno + " " + name);
}
}
class strt
{
static void Main(string[] args)
{
Student s = new Student();
s.SetData(1, "Enrique");
s.ShowData();
Student t = s; // values of s will be copied into t
t.ShowData();
t.SetData(2, "Atif"); // s will not change
t.ShowData();
s.ShowData();
Console.ReadLine();
}
}
}
19
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
20
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 11. WAP to show how we can pass object as an argument to a function.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication13
{
class Student
{
int rollno;
string name;
public void SetData(int r, string n)
{
rollno = r;
name = n;
}
public void ShowData()
{
Console.WriteLine(rollno + " " + name);
}
public void SetData(Student a)
{
rollno = a.rollno;
name = a.name;
}
}
class Demo
{
static void Main(string[] args)
{
Student s = new Student();
s.SetData(1, "Shirlee");
s.ShowData();
Student t = new Student();
t.SetData(s);
t.ShowData();
t.SetData(2, "Robin"); // s will not change
t.ShowData();
s.ShowData();
Console.ReadLine();
}
}
}
21
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
22
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 12. WAP that will work like copy constructor of c++.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication14
{
class Student
{
int rollno;
string name;
23
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
24
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 13. WAP that contains different classes but no inheritance.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication15
{
class A
{
public void Show()
{
Console.WriteLine("Show A");
}
}
class B
{
public void Show()
{
Console.WriteLine("Show B");
}
}
class Demo
{
static void Main(string[] args)
{
A a = new A();
a.Show();
B b = new B();
b.Show();
Console.ReadLine();
}
}
}
25
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
26
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 14. WAP to inherit base class into derived class (no method overriding).
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication16
{
class A
{
public void Say()
{
Console.WriteLine("say A");
}
public void Write()
{
Console.WriteLine("write A");
}
}
class B : A
{
public void Say()
{
Console.WriteLine("say B");
}
public void Call()
{
Console.WriteLine("Call B");
}
}
class Demo
{
static void Main(string[] args)
{
A a = new A();
Console.WriteLine("Langston Hughes");
a.Say();
a.Write();
//a.call(); // call() not accessible by A's abject
Console.WriteLine("*Octavia Butler");
B b = new B();
b.Say();
b.Write();
b.Call();
Console.WriteLine("--when (a=b)--");
a = b;
27
Name: Tanmay Baid
Roll
Number: 0829cs071112
a.Say();
a.Write();
// a.Call(); // we cant call call() bcozz it is not defined by A class
Console.ReadLine();
}
}
}
28
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
29
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 15. WAP to show polymorphism using virtual and override keyword .
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication17
{
class A
{
public virtual void Show()
{
Console.WriteLine("Show A");
}
public virtual void Fun()
{
Console.WriteLine("Fun A");
}
}
class B : A
{
public override void Show()
{
Console.WriteLine("Show B");
}
public void Call()
{
Console.WriteLine("Call B");
}
}
class Demo
{
static void Main(string[] args)
{
A a = new A();
Console.WriteLine("***X***");
a.Show();
a.Fun();
//a.Call(); // call() not accessible by A's abject
B b = new B();
Console.WriteLine("***Y***");
b.Show();
b.Fun();
b.Call();
a = b;
Console.WriteLine("***when(a=b)***");
30
Name: Tanmay Baid
Roll
Number: 0829cs071112
a.Show();
a.Fun();
// a.Call(); // we cant call call() bcozz it is not defined by A class
Console.ReadLine();
}
}
}
31
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
32
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 16. WAP to show how we can use abstract class and abstract method.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication18
{
abstract class A
{
public virtual void Show()
{
Console.WriteLine("Show A");
}
public virtual void Fun()
{
Console.WriteLine("Fun A");
}
public abstract void Call();
}
class B : A
{
public override void Show()
{
Console.WriteLine("Show B");
}
public override void Call()
{
Console.WriteLine("Call B");
}
}
class Demo
{
static void Main(string[] args)
{
B b = new B();
Console.WriteLine("----B----");
b.Show();
b.Fun();
b.Call();
A a = b;
Console.WriteLine("----(A=B)----");
a.Show();
a.Fun();
a.Call();
Console.ReadLine();
}
33
Name: Tanmay Baid
Roll
Number: 0829cs071112
}
}
Output:
34
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 17. WAP to show multiple inheritance using class and interface.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication19
{
class A
{
public virtual void Show()
{
Console.WriteLine("Show A");
}
public void Disp() { }
}
interface Z
{ void Call();
}
class B : A, Z
{
public override void Show()
{
Console.WriteLine("Show B");
}
public void Call()
{
Console.WriteLine("Call B");
}
}
class Demo
{
static void Main(string[] args)
{
B b = new B();
Console.WriteLine("----B---");
b.Show(); // will call show() of B class
b.Call(); // will call call() of B class
A a = b;
Console.WriteLine("---(A=B)--");
a.Show(); // will call show() of B class
Console.ReadLine();
}
}
}
35
Name: Tanmay Baid
Roll
Number: 0829cs071112
Output:
36
Name: Tanmay Baid
Roll
Number: 0829cs071112
// 18. WAP to avoid inheritance of particular method.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication20
{
class A
{
public virtual void Show()
{
Console.WriteLine("Show A");
}
}
class B : A
{
public sealed override void Show()
{
Console.WriteLine("Show B");
}
}
class C : B
{
public override void Show() // Error
:sealed method in B cant be override
{
Console.WriteLine("Show B");
}
}
class Demo
{
static void Main(string[] args)
{
B b = new B();
A a = b;
a.Show();
Console.ReadLine();
}
}
}
37
Name: Tanmay Baid
Roll
Number: 0829cs071112
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication21
{
class Colors
{
enum Color
{
Voilet,
Green,
Blue,
}
static void Main(string[] args)
{
string buffer;
Color myColor;
Console.WriteLine("Violet=0");
Console.WriteLine("Green=1");
Console.WriteLine("Blue=2");
Console.WriteLine("Enter a value for a color:");
buffer = Console.ReadLine();
myColor = (Color)Convert.ToInt32(buffer);
switch (myColor)
{
case Color.Voilet:
System.Console.WriteLine("\nYour choise of colour is:...");
break;
case Color.Green:
System.Console.WriteLine("\nYour choise of colour is...");
break;
case Color.Blue:
System.Console.WriteLine("\nYour choise of colour is...");
break;
default:
System.Console.WriteLine("\nYour choise of colour is...");
break;
}
38
Name: Tanmay Baid
Roll
Number: 0829cs071112
System.Console.WriteLine("\n {0} ({1})", myColor, (int)myColor);
Console.WriteLine("thanks!!");
Console.Read();
}
}
}
Output:
39
Name: Tanmay Baid
Roll
Number: 0829cs071112
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication22
{
class TTT
{
public enum Mode
{
off,
on
}
static void Main(string[] args)
{
Mode ob;
Console.WriteLine("enter 0 or 1");
string str = Console.ReadLine();
ob = (Mode)Convert.ToByte(str);
if (ob == Mode.off)
Console.WriteLine("off");
40
Name: Tanmay Baid
Roll
Number: 0829cs071112
else if (ob == Mode.on)
Console.WriteLine("on");
else
Console.WriteLine("invalid number");
Console.ReadLine();
}
}
}
Output:
41
Name: Tanmay Baid
Roll
Number: 0829cs071112
42