10
Homework Title / No. : __ Modern Programming tool and techniques Course Code : __ CAP406  _______ Course Instructor : ____ Miss RamanDeep  ____ Course Tutor (if applicable) : _ Date of submission : ___25-9-10____ Student’s Roll No. E3804A23 Section No. : E3804  Declaration: I declare that this assignment is my individual work. I have not copied from any other student’s work or from any other source except where due acknowledgment is made explicitly in the text, nor has any part been written for me by another person. Student’s Signature : ___Gaurav Kakkar__________  Evaluator’s comments:  _____________________________________________________________________ Marks obtained : ___________ out of ______________________ Assignment 2 Page 1

2C SHARP

Embed Size (px)

Citation preview

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 1/10

Homework Title / No. : __ Modern Programming tool and techniques

Course Code : __ CAP406 _______

Course Instructor : ____ Miss RamanDeep ____

Course Tutor (if applicable) : _ Date of submission : ___25-9-10____

Student’s Roll No. E3804A23 Section No. : E3804 Declaration:I declare that this assignment is my individual work. I have not copied from any other student’s work or fromany other source except where due acknowledgment is made explicitly in the text, nor has any part beenwritten for me by another person .

Student’s Signature : ___Gaurav Kakkar__________

Evaluator’s comments: _____________________________________________________________________

Marks obtained : ___________ out of ______________________

Assignment 2 Page 1

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 2/10

PART A

1. Write a Program that implements Date and Time Functions.

ANS:-

using System;namespace ConsoleApplication1{

class b{

static void Main(string[] args){

DateTime dt = DateTime.dt;Console.WriteLine("the data and time is : {0}", now);Console.ReadLine();

}}

}

OUTPUT

The date and time is : 9/25/2010 8:22:12 PM

2 Write a Program to check whether a string is palindrome or not.

ANS:-

using System;

namespace ConsoleApplication2{

class c{

static void Main(string[] args)

{ string s = string.Empty;

Console.WriteLine("Enter String here");

string s1 = Console.ReadLine();

int i = s1.Length;

Assignment 2 Page 2

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 3/10

for (int j = i - 1; j >= 0; j--){

s = s + s[j];}

if (s == s1)

{

Console.WriteLine("This srting is Palindrome");

}

else{

Console.WriteLine("this string is Not a palindeome");}

Console.WriteLine(s);

Console.Read();

}}

}

Output:Enter String hereGauravthis string is Not a palindeome

3 Write a program that implements mathematical functions.

ANS:-

:

using System;

class MainClass {

public static void Main() {

Assignment 2 Page 3

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 4/10

Double num;

for (num = 0.1; num <= 1.0; num = num + 0.1) {

Console.WriteLine("Sine of " + num + " is " +Math.Sin(num));

Console.WriteLine("Cosine of " + num+ " is " + Math.Cos(num));Console.WriteLine("Tangent of " + num + " is " + Math.Tan(num));

Console.WriteLine();

}

}

}

4 Using Classes, Objects & methods, write a Program that performs various arithmetic operations

on numbers.

ANS:-

using System;

namespace ConsoleApplication1{

class aa{

class r {

public int l, w;public void input(int x,int y){l=x;w=y;

}public int areamet()

{int a=l*w;return(a);

}

public int subs(){

int s = w - l;return (s);

}

Assignment 2 Page 4

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 5/10

public int addit(){

int ad = w + l;return (ad);

}public int divid(){

int div = w / l;return (div);

}};

static void Main(string []a){

int a1,s1,ad1,div1;r r1=new r();r r2=new r();

r1.l=50;r2.w=60;a1=r1.l*r2.w;ad1 = r1.l + r2.w;s1 = r2.w - r1.l;div1 = r2.w / r1.l;Console.WriteLine("the Substract (60 and 50)is------->" + s1);Console.WriteLine("Additition (60 and 50)is------->" + ad1);Console.WriteLine("Areamet (50 and 60) is ------->"+a1);Console.WriteLine("Division (60 and 50)is ------->" + div1);Console.ReadLine();

}}

}

5 Write a program to compute sum of all the elements stored in a single dimension array using

functions.

ANS:-

using System;

namespace ConsoleApplication1{

class m{

Assignment 2 Page 5

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 6/10

static void Main(string[] args){

int[] a = new int[10];int i, b,y=0;Console.WriteLine("the size of array is");

i = Convert.ToInt32(Console.ReadLine());Console.WriteLine("the elements of array enter here");for (b = 1; b <= i; b++){

a[b] = Convert.ToInt32(Console.ReadLine());y = y + a[b];

}Console.WriteLine("sum of array "+y);Console.ReadLine();

}

}}

PART B

1. Write a program with functions to find the factorial of a number using recursion.

ANS:-

using System;

namespace fact{

class l{

static void Main(string[] args){

int i;Console.WriteLine("Enter the number for factorial");

i = Convert.ToInt32(Console.ReadLine());

long a=Factorial(i);Console.WriteLine("the Factorial of the number is---->"+a);Console.ReadLine();

}static long Factorial(long number)

{if (number <= 1)

Assignment 2 Page 6

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 7/10

return 1;else

return number * Factorial(number - 1);}

}

}

2. Create a class using methods to store and print general information of a customer.

ANS:-

using System;

namespace ConsoleApplication1{

class P{

class o{

public void str(){

int b;string a = "";

Console.WriteLine("here Customer name ?");

a = Console.ReadLine();Console.WriteLine("the items ?");int i = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("cost=10 rs/per item:");b = i * 10;

Console.WriteLine("CUSTOMER name" + a);Console.WriteLine("");Console.WriteLine("ttl cost”+ b);

}

};static void Main(string[] a){

o oo = new o();

Assignment 2 Page 7

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 8/10

oo.str();Console.ReadLine();

}

}}

3. Write a program with multiple classes to show the reusability of code.

ANS:-

using System;

namespace ConsoleApplication1{

class P{

class roomcls{

public int l;public int b;public roomcls(int x, int y){

l = x;b = y;

}public int ar(){

return (l * b);}

}class bedroomcls : roomcls{

int h;public bedroomcls(int x, int y, int z)

: base(x, y){

h = z;}public int vol(){

return (l * b * h);}

}class inher {

static void Main(string[] args){

Assignment 2 Page 8

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 9/10

bedroomcls roomcls1 = new bedroomcls(14, 12, 10);int ar1 = roomcls1.ar();int vol1 = roomcls1.vol();Console.WriteLine("Ar1=" + ar1);Console.WriteLine("Vol1=" + vol1);Console.ReadLine();

}

}}

}

4. Write down various access identifiers used in C#.

ANS:-

public - The member can be accessed from anywhereinternal - The member can only be accessed from type it originates from or other types in the same assembly

protected - The member can only be accessed from the type it originates from or from detrieved types of the

originating type protected internal - implies protected OR internal ( not protected AND internal )

private - The member is only accessible by other members within the type it originates from.

5. Can you inherit from multiple classes in C#? If yes then justify your answer by giving suitable

example and if no then give reason .

ANS:- “ We can not inherit from multiple classes in C#. But, it can possible by multiple interfaces. ”

Example:-

using system;

namespace consoleapplication1{

class p{

interface addition{int addition();

}interface multipl{

int multipl();}

Assignment 2 Page 9

8/8/2019 2C SHARP

http://slidepdf.com/reader/full/2c-sharp 10/10

class calcul : addition, multipl{

int x, y;public calcul(int x, int y){

this.x = x;this.y = y;

}public int a(){

return (x + y);}public int m(){

return (x * y);}

}

class in{

public void main(string[] args){

calcul com = new calcul(10, 20);addition object1 = (addition)com;console.writeline("the sum=" + object1.a());multipl object2 = (multipl)com;console.writeline("the product=" + object2.multipl());

console.readline();

}}

}

Assignment 2 Page 10