Вы находитесь на странице: 1из 11

1.

Design a class named Date with the following members: a. Data Member day, month and year b. A constructor to provide a value to data member. c. A method to change the date. d. A method to display date in the format mm-dd-yyyy.

Soln: using System; class date1 { int day; int month; int year; date1(int d,int m, int y) { day=d; month=m; year=y; Console.WriteLine(m+"/"+d+"/"+""+y); } public void show(string str) { string s=str; string[] words=s.Split('/'); Console.WriteLine("date in mm/dd/yy format is :"+words[1]+"/"+words[0]+"/"+words[2]); } public void change() { string str; Console.WriteLine("change date"); Console.WriteLine("enter new date to change the date"); str=Console.ReadLine(); show(str); } public static void Main() { date1 da=new date1(1,2,1322); da.change(); } }

2.

Define a class named Employee that contain the following member

a. Data member to store information of employee(name, id,dept, salary) b. A method to calculate and display salary of given employee. c. Define properties for the data member to access their value. Soln: using System; class employee { private string pname; private int id; public string name { get { return pname; } set { pname=value; } } public int eid { get { return id; } set { id=value; } } public double salary() { double bs,salary, hra, da; Console.WriteLine("insert hra+da+basicsalary"); bs=Convert.ToDouble(Console.ReadLine()); hra=Convert.ToDouble(Console.ReadLine()); da=Convert.ToDouble(Console.ReadLine()); salary=hra+da+bs; return salary; }

public static void Main() { employee e= new employee(); Console.WriteLine("enter your name and id"); e.name=Console.ReadLine(); e.id=Convert.ToInt32(Console.ReadLine()); string s=e.name; int s2=e.id; double salary=e.salary(); Console.WriteLine("your name is "+s+"\nyour id is "+s2+"\nyour salary is "+salary); } } 3. Write a program that compute the sum of the digits of given integer number. Soln: using System; class digits { public static void Main() { Console.WriteLine("enter the number to calculate the sum of all its digits"); int d=Convert.ToInt32(Console.ReadLine()); int s=0, n=0; while(d!=0) { n=d%10; s=s+n; d=d/10; } Console.WriteLine("sum of al digits is "+s); } } 4. Write a method Prime that return true if its argument is a prime number and return false otherwise. Soln:

using System; class prime { public bool show(int a) { int c=0; for(int i=2; i<a/2;i++) { if(a%i==0) { c=c+1; break; } } if (c==0) { return true; } else return false; } public static void Main() { prime p=new prime(); int b=Convert.ToInt32(Console.ReadLine()); if (p.show(b)) { Console.WriteLine("number is prime"); } else { Console.WriteLine("number is not prime"); } } } 5. Write a method that takes an array as an input parameter and use two method, one to find the largest array element and other to compute the average of array element. Soln: using System;

class inputarray { void show(params int[] a) { int s=a[0],c=0,sum=0; foreach(int i in a) { c=c+1; sum=sum+i; if(i>s) { s=i; } } Console.WriteLine("sum of all number is "+s); average(sum, c); } void average(int a, int b) { int c=a/b; Console.WriteLine("average of the numbers in array is "+c); } public static void Main() { inputarray ia=new inputarray(); ia.show(10,20,30); } }

6. The annual examination result of 10 student are tabulated as follow: Roll_No Subject1 Subject2 Subject3 a. Total marks obtained by each subject. b. The highest marks in each subject and the Roll_Number of the student who secured. c. The student who obtained highest total marks. Soln:

using System; class assgn26 { public static void Main() { int i=0; int r1=0,r2=0,r3=0,r4=0; float max1=0,max2=0,max3=0,max4=0; float[] total = new float[10]; int[] r={1,2,3}; float[] s1={10,20,15}; float[] s2={15,19,18}; float[] s3={12,10,15}; for(i=0;i<3;i++) { Console.WriteLine("NO " + (i+1)); Console.WriteLine("Marks Of student"); Console.WriteLine("Roll NO " + r[i]); Console.WriteLine("Subject 1 " + s1[i]); Console.WriteLine("Subject 2 " + s2[i]); Console.WriteLine("Subject 3 " + s3[i]); total[i]=s1[i]+s2[i]+s3[i]; Console.WriteLine("Total is " + total[i]); } for(i=0;i<3;i++) { if(s1[i]>max1) { max1=s1[i]; r1=r[i]; } if(s2[i]>max2) { max2=s2[i]; r2=r[i]; } if(s3[i]>max3) { max3=s1[i]; r3=r[i]; } if(total[i]>max4) { max4=total[i];

r4=r[i]; } } Console.WriteLine("Highest + r1); Console.WriteLine("Highest + r2); Console.WriteLine("Highest + r3); Console.WriteLine("Highest } }

marks in subject 1 are " + max1 + " scored by roll no " marks in subject 2 are " + max2 + " scored by roll no " marks in subject 3 are " + max3 + " scored by roll no " Total Marks are " + max4 + " scored by " + r4);

7. Write a program that accept a shopping list of five items from the command line store them in a string type array and then print the list in the alphabetical order. Soln: using System; class shop { public static void Main(string[] args) { string[] listname=new string[5];//{"dognut","elichi","apple", "banana"}; listname[0]=args[0]; listname[1]=args[1]; listname[2]=args[2]; listname[3]=args[3]; listname[4]=args[4]; string[] s=new string[5]; for(int i=0; i<listname.Length; i++) { s[i]=listname[i]; for(int a=i+1;a<listname.Length;a++) { if(string.Compare(listname[a],s[i])<0) { s[i]=listname[a]; listname[a]=listname[i]; listname[i]=s[i]; }

} }

Console.WriteLine("\n"+s[0]+"\n"+s[1]+"\n"+s[2]+"\n"+s[3]+"\n"+s[4]); } }

8. (Refer to program No7) Write a method that would check whether the given element is present in the array. If the present value is present in array it should return true or else false. Soln: using System; class arraytype { bool show(params string[] s) { Console.WriteLine("enter an element to chck if it exits"); string chck=Console.ReadLine(); int c1=0; foreach(string c in s) { if (string.Compare(chck,c)==0) { c1=c1+1; break; } else { c1=0; break; } } if(c1==1) { return true; } else { return false; }

} public static void Main(string[] args) { string[] listname=new string[5]; listname[0]=args[0]; listname[1]=args[1]; listname[2]=args[2]; listname[3]=args[3]; listname[4]=args[4]; arraytype a= new arraytype(); if(a.show(listname[0],listname[1],listname[2],listname[3],listname[4])) { Console.WriteLine("exits"); } else { Console.WriteLine("does not exits"); } } }

9. Write a program that count the number of occurance of a particular character in a line of a text. Soln: using System.Text.RegularExpressions; using System; using System.Collections; class regu1 { public static void Main() {

Console.WriteLine("What you want to search"); string a = Console.ReadLine(); Regex r = new Regex(a); Console.WriteLine("Enter the string you want to search"); string b= Console.ReadLine(); MatchCollection m= r.Matches(b); Console.WriteLine(m.Count); Console.ReadLine(); } } 10. Define a base class called Animal with the following member a.A String type data member to store the name of the animal. b. An integer member to store the age of the animal. c. A method to display the name and age of the animal. Derive two classes named Cat and Dog from Animal. Then write a driver program to create Cat and Dog object with suitable value. Display the content of the object by calling the display method on the derived class. Soln: using System; class Animal { public string name; public int age; public void enter(string n,int a) { name=n; age=a; } } class Cat:Animal { public void display()

{ Console.WriteLine("CAT"); Console.WriteLine("Name of the cat is " + name); Console.WriteLine("Age is " + age); } } class Dog:Animal { public void display() { Console.WriteLine("Dog"); Console.WriteLine("Name of the cat is " + name); Console.WriteLine("Age is " + age); } } class assgn210 { public static void Main() { Cat c=new Cat(); Dog d=new Dog(); Console.WriteLine("Enter the type of animale? cat or dog?"); string type=Console.ReadLine(); Console.WriteLine("Enter name and age of the animal"); string name=Console.ReadLine(); int age=Int32.Parse(Console.ReadLine()); if(type =="Cat" || type=="CAT" || type=="cat") { c.enter(name,age); c.display(); } else { d.enter(name,age); d.display(); } }}

Вам также может понравиться