Vous êtes sur la page 1sur 6

Write a program in C# using command line arguments to display a welcome message.

The message has to be supplied as input in the command line. using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { for(int i=0 ; i < args.Length ; i++) { Console.Write(args[i] + ""); } Console.ReadLine(); }

Write a program in C# to find the area of polygon triangle and rectangle by using multiple constructors. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Area { public Area(double Base , double Height) // Constructor for triangle { Double b = Base ; Double h = Height ; Double tri_area = (0.5) * b *h ; Console.WriteLine("Area of Triangle :" + tri_area); }

} public Area(float width , float height) // constructor for rectangle Write a program in C# to find the area of a circle, given its radius. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("Enter the radius of Circle to find its area"); Double radius = Double.Parse(Console.ReadLine()); Double area = Math.PI *radius * radius; Console.WriteLine("The area of circle for given radius is : " + area); Console.ReadLine(); Console.WriteLine("--------For Rectangle-------"); } Console.WriteLine("Enter the width of the rectangle"); float rec_width = (float) (Convert.ToDouble(Console.ReadLine())); { float w = width; float h = height; float rec_area = w * h ; Console.WriteLine("Area of Rectangle : " + rec_area); } public static void Main(string [] args) { Console.WriteLine(" --------For Triangle-------"); Console.WriteLine("Enter the base of triangle: "); Double tri_base= Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter the height of the triangle :"); Double tri_height = Convert.ToDouble(Console.ReadLine());

} Console.WriteLine("Enter the height of rectangle :"); } float rec_height = (float) (Convert.ToDouble(Console.ReadLine()));

Console.WriteLine("---------Result---------");

Area A1 = new Area(tri_base , tri_height);

Area A2 = new Area(rec_width, rec_height);

} }

Console.ReadLine();

Using foreach looping statement, write a program in C# to display all the elements of string array. using System;

using System.Collections.Generic; using System.Text;

Write a program in C# to add and multiply two numbers and display the results. Demonstrate the use of ref and out parameters in this program. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { public static int ADD(ref int x, ref int y) { int res = x + y; return res; } public static int Multiply(int x, int y, out int res1) { res1 = x * y; return res1; } static void Main(string [] args) { Console.WriteLine("Enter first Number"); int n1 = Convert.ToInt32(Console.ReadLine());

namespace ConsoleApplication1 { class Program { static void Main (string [] args) { String [] str = {"I", "LOVE", "MY", "INDIA"}; foreach(string s in str ) { Console.WriteLine(s); } Console.ReadLine(); } }

Write a program in C# to demonstrate the usage of checked and unchecked statements. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program

Console.WriteLine("Enter Second Number"); int n2 = Convert.ToInt32(Console.ReadLine());

{ public static void Main() {

int Res = Program.ADD(ref n1, ref n2); Console.WriteLine("Sum of two number is : " + Res);

int I = Int32.MaxValue; Console.WriteLine("Simple : " + (I + 1));

Program.Multiply(n1 , n2, out Res); // Res is responsible to hold return value which is n1*n2.

unchecked { Console.WriteLine("Unchecked" + (I + 1)); }

Console.WriteLine("Multiply of two number is : " + Res); 2 Console.ReadLine();

checked { try { Console.WriteLine(I+ 1); } catch { Console.WriteLine(""); } } Console.ReadLine(); } } } }

Write a program for matrix multiplication in C#. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string [] args) { int [,] x = {{1,2,3} , {4,5,6} , {7,8,9}}; int [,] y = {{11,12,13} , {14,15,16}, {17,18,19}};

Write a class called rectangle which consists of members side_1, side_2 and displayArea(). Write another class square which inherits class rectangle. Take side of square as input and display the area of square on console. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { public double side_1; public double side_2; public void DisplayArea() { Double area = side_1 * side_2 ; Console.WriteLine("Area is : " + area); } } class Square { public static void Main(string [] args) { Program P = new Program(); Console.WriteLine("Enter the side of Square : "); P.side_1 = Convert.ToDouble(Console.ReadLine()); P.side_2 = P.side_1; P.DisplayArea(); 3 } Console.ReadLine(); for(int i = 0 ; i < 3 ; i++) { for(int j = 0 ; j < 3 ; j++ ) { Console.WriteLine("Multiplication of two matrix (Matrix1 * Matrix2)"); } } for(int i = 0 ; i < 3 ; i++) { for(int j = 0 ; j < 3 ; j++ ) { Console.Write(x[i , j] + ""); Console.WriteLine(); } Console.WriteLine("---------Second Matrix----------"); } for(int i = 0 ; i < 3 ; i++) { for(int j = 0 ; j < 3 ; j++ ) { Console.Write(x[i , j] + ""); Console.WriteLine(); Console.WriteLine("---------First Matrix----------");

Console.Write(x[i , j] * y[i, j] + ""); Console.WriteLine(); } } Console.ReadLine(); } } } }

Display(arr); Console.ReadLine(); }

Write a program in C# to demonstrate operator overloading. using System;

using System.Collections.Generic; using System.Text;

Write a program in C# to sort the students list based on their names. The students list is stored in a string array. using System;

namespace ConsoleApplication1 { class Complex

using System.Collections.Generic; { using System.Text; private int x; namespace ConsoleApplication1 private int y; { public Complex() class Program { { } public static void Display(Array arr) public Complex(int i, int j) { { foreach(string s in arr) x = i; { y = j; Console.WriteLine(s); } } public void ShowXY() } { static void Main(string [] args) Console.WriteLine("{0} {1}",x,y); { } Console.WriteLine("Enter the Number of Students"); public static Complex operator -(Complex c) int n = Convert.ToInt32(Console.ReadLine()); { Complex temp = new Complex(); String [] arr = new string [n]; temp.x = -c.x; temp.y = -c.y; for(int I = 0 ; I < arr.Length ; I ++) return temp; { } Console.WriteLine("Enter" + (I + 1) + "Student Name"); } String str = Console.ReadLine(); arr.SetValue(str , I ); class MyClient } { Console.WriteLine("\n The Name You Have entered is sort order is given below"); public static void Main() { 4 Console.WriteLine(); Complex c1 = new Complex(10,20); Array.Sort(arr); c1.ShowXY(); // displays 10 & 20

Complex c2 = new Complex(); c2.ShowXY(); // displays 0 & 0 c2 = -c1; c2.ShowXY(); // diapls -10 & -20 Console.ReadLine(); } }

try { int n1 = 10; int n = 0; int res; res = n1/n;

Console.WriteLine(res); } } catch(Exception ex) { Write a program in C# to demonstrate boxing and unboxing opertation. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string [] args) { int i = 10; Console.WriteLine("Value of i : " + i); object obj = (object)(i + 1); // boxing convert into object Console.WriteLine("Value of obj : " + obj); Write a program to demonstrate the usage of threads in C#. using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace ConsoleApplication1 i = (int)obj; Console.WriteLine("Value of I after unbox : " + i); Console.ReadLine(); } } { class Program { public void get1() { for(int i=0; i<= 10 ; i++) } { Console.WriteLine("Welcome"); Write a program in C# to throw and catch any arithmetic exception. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { 5 static void Main(string [] args) { } Thread.Sleep(1000); } Console.ReadLine(); } } Console.WriteLine(ex.Message.ToString());

} public void get2() { for(int j = 0; j<=10 ; j++) { Console.WriteLine("india"); Thread.Sleep(1000);

} } public static void Main(string [] args) { Program P = new Program(); ThreadStart TS1 = new ThreadStart(P.get1); ThreadStart TS2 = new ThreadStart(P.get2);

Thread T1 = new Thread(TS1); Thread T2 = new Thread(TS2);

T1.Start(); Thread.Sleep(500); T2.Start(); } }

Vous aimerez peut-être aussi