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

Lab File: Jamia Hamdard University

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

INTRODUCTION TO .

NET PROGROMMING
LAB FILE

A DISSERTION/PROJECT REPORT

Submitted by
YASIR AHMED
2014-301-070

In partial fulfilment for the award of the degree of

BACHELOR OF COMPUTER APPLICATION

Under the supervision of


MR. ZUBAIR AHMED SHAH

JAMIA HAMDARD UNIVERSITY


New Delhi-110062
Q1) Write the program of sum of 1st n prime numbers in C# ?
Ans.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int num, i = 3, count, c, sum = 0;
int flg;
Console.WriteLine("Enter the number of prime numbers you want");
num = Convert.ToInt32(Console.ReadLine());
if (num >= 1)
{
Console.WriteLine("First " + num + " Prime Numbers are :\n");
Console.WriteLine("2");
sum = 2;
}
for (count = 2; count <= num - 1;)
{
flg = 1;
for (c = 2; c <= i - 1; c++)
{
if (i % c == 0)
flg = 0;
break;
}
if (flg == 1)
{
Console.WriteLine("\n" + i);
sum = sum + i;
Count++;
}
i++;
}
Console.WriteLine ("sum=" + sum);
Console.ReadKey ();
}
}
}
Q2) Write the program of Single inheritance in C# ?
Ans.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Teacher d = new Teacher();
d.Teach();
Student s = new Student();
s.Learn();
s.Teach();
Console.ReadKey();
}
class Teacher
{
public void Teach()
{
Console.WriteLine("Teach");
}
}
class Student : Teacher
{
public void Learn()
{
Console.WriteLine("Learn");
}
}
}
Q3.) Write the program of function overloading in C#?
Ans.

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

namespace ConsoleApplication5
{
class Man
{
public int sum(int A, int B)
{
return (A + B);
}
public float sum(float A, float B)
{
return (A + B);
}
}
class addition
{
public static void Main(string[] args)
{
Man X1 = new Man();
Console.WriteLine(X1.sum(10, 20));
Console.WriteLine(X1.sum(7.5f, 9.5f));
Console.ReadKey();
}
}
}
Q4.Write the program to demonstrate the use of constructor and
destructor in C#?
Ans.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
class SampleA
{
// Constructor
public SampleA()
{
Console.WriteLine("An Instance Created");
}
// Destructor
~SampleA()
{
Console.WriteLine("An Instance Destroyed");
}
}
class Program
{
public static void Test()
{
SampleA T = new SampleA(); // Created instance of class
}
static void Main(string[] args)
{
Test();
GC.Collect();
Console.ReadLine();
}
}
}

OUTPUT:
Q5) Write the program to Bubble Sort in C#?
Ans.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
int i, n, temp, j;
int[] arr=new int[8];
public void read()
{
Console.WriteLine("enter the no of elements");
n = int.Parse(Console.ReadLine());
Console.WriteLine("enter the elements");
for(i=0; i<n;i++)
arr[i]=int.Parse(Console.ReadLine());
}
public void BubbleSort()
{
for(i=0; i<n;i++)
{
for(j=0; j<n-i ; j++)
{
if(arr[j] > arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
public void Display()
{
for(i=1; i<n+1;i++)
{
Console.WriteLine(arr[i]);
}
}
static void Main(string[] args)
{
Program pro = new Program();
pro.read();
pro.BubbleSort();
pro.Display();
Console.ReadKey();
}
}
}
Q6) Write the program of Operator Overloading in C# ?
Ans.

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

namespace ConsoleApplication7
{
class operators
{
int length;
int breath;
public void input(int x, int y)
{
length = x;
breath = y;
}

public void display()


{
Console.WriteLine("length =" +length);
Console.WriteLine("breath =" + breath);
}
public static operators operator+ (operators m, operators n)
{
operators o = new operators();
o.length = m.length + n.length;
o.breath = m.breath + n.breath;
return o;
}
}
class Program
{
static void Main(string[] args)
{
operators rec1 = new operators();
operators rec2 = new operators();
operators rec3 = new operators();
rec1.input(3, 2);
rec2.input(2, 1);
rec3 = rec1 + rec2;
rec3.display();
Console.ReadKey();
}
}
}

Q7) Write the program of Binary Search in C#?


Ans.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int[] numArray = new int[100];
bool isNum = false;
int sizeNum;
Console.WriteLine("Enter the limit of array:");
string sizeString = Console.ReadLine();
isNum = Int32.TryParse(sizeString, out sizeNum);
if (isNum)
{
Console.WriteLine("Enter array values (numeric only) in ascending
order. ");
for (int i = 0; i < sizeNum; i++)
{
int tempValue;
string arrayItem = Console.ReadLine();
isNum = Int32.TryParse(arrayItem, out tempValue);
if (isNum)
{
numArray[i] = tempValue;
}
else
{
Console.WriteLine("You enters a non-numeric value!");
break;
}
}
Console.WriteLine("Enter search value (numeric only).");
int searchNum;
string searchString = Console.ReadLine();
isNum = Int32.TryParse(searchString, out searchNum);
if (isNum)
{
int lowNum = 0;
int highNum = sizeNum - 1;
while (lowNum <= highNum)
{
int midNum = (lowNum + highNum) / 2;
if (searchNum < numArray[midNum])
{
highNum = midNum - 1;
}
else if (searchNum > numArray[midNum])
{
lowNum = midNum + 1;
}
else if (searchNum == numArray[midNum])
{
Console.WriteLine("Search successful");
Console.WriteLine("Value {0} found at location {1}\n",searchNum, midNum + 1);
Console.ReadLine();
return;
}
}
Console.WriteLine("Value {0} was not found \n", searchNum);
Console.ReadLine();
return;
}
else
{
Console.WriteLine("Search value must be numeric!");
Console.ReadLine();
return;
}
}
else
{
Console.WriteLine("You must enter a numeric value!");
Console.ReadLine();
}
Console.ReadLine();
}
}
}

Q8.Write the program to input the roll no.,name,marks of ten student


from user and print the rollno. & name of the topper in c#?
Ans.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
int x, k, y;
String z;
Test[] ts = new Test[10];
for (int j = 0; j < 3; j++)
{
ts[j] = new Test();
}
for (int i = 0; i < 3; i++)
{
ts[i].input();
ts[i].output();
}

x = ts[0].marks;
y = ts[0].roll;
z = ts[0].n;
for (k = 0; k < 3; k++)
{
if (x < ts[k].marks)
{
x = ts[k].marks;
y = ts[k].roll;
z = ts[k].n;
}
}
Console.WriteLine("name of topper is" + z);
Console.WriteLine("roll no of topper is" + y);
Console.ReadKey();
}
class Test
{
public int roll;
//char[] name = new char[20];
public String n;
public int marks;
public void input()
{
Console.WriteLine("enter roll no, name, and mark");
roll = int.Parse(Console.ReadLine());
n = Console.ReadLine();
marks = int.Parse(Console.ReadLine());
}
public void output()
{
Console.WriteLine("displaying student roll no " + roll);
Console.WriteLine("displaying student name " + n);
Console.WriteLine("displaying student marks " + marks);
Console.ReadKey();
}
}
}
}

Q9)Write the program to check whether the string a Palindrome or not


in C#?

Ans.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome \n Entered String Was {0} and reverse
string is {1}", s, revs);
}
else
{
Console.WriteLine("String is not Palindrome \n Entered String Was {0} and
reverse string is {1}", s, revs);
}
Console.ReadKey();
}
}
}

Q10) Write a program to input email id and password of user and


checking whether email if contains @ and whether password is
at least 6 character long.
Ans.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>

<script runat="server">
private void func1(object sender, EventArgs a)
{
string x, y;
x = t1.Value;
y = t2.Value;
int l = x.Length;
int l2 = y.Length;
for (int i = 0; i < l; i++)
{
if (x[i] == '@')
{
t1out.Value = "@ is present";
t1.Value = "";
}
else
{
t1out.Value = "@ is not present";
t1.Value = "";
}
}
if(l2>6)
{
t2out.Value = "password is greater then 6";
t2.Value = "";
}
else
{
t2out.Value = "password is not greater then 6";
t2.Value = "";
}
}
</script>
<html>
<head runat="server">
<title>Fom Validation</title>
</head>
<body>
<form id="Form1" runat="server">
Login<br/>
Email: <input runat="server" type="text" id="t1"/> out <input runat="server" type="text"
id="t1out"/>
<br/><br/>
Password:<input runat="server" type="password" id="t2"/> out <input runat="server" type="text"
id="t2out"/>
<br><br>
<input id="Button1" type="button" runat="server" value="Login" onserverclick="func1" />
</form>
</body>
</html>
OUT PUT:

You might also like