Example 1: Write A Program To Sort One Dimensional Array In Ascending Order Using Static Method in C#.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; namespace Sort_Array { public class Program { public static void Main(string[] args) { int[] num= {22,50,11, 2, 49}; Array.Sort(num); for(int i=0; i<num.Length; i++) { Console.Write(num[i] + " "); } } } } |
Example 2: Write A Program To Print One Dimensional Array In Reverse Order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; namespace Sort_Array_Des { public class Program { public static void Main(string[] args) { int[] num= {22,50,11, 2, 49}; Array.Reverse(num); for(int i=0; i<num.Length; i++) { Console.Write(num[i] + " "); } } } } |
Example 3: Write A Program To Sort One Dimensional Array In Descending Order Using Non Static Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System; namespace Sort_Array_Des { public class Program { public static void Main(string[] args) { int[] num= {22,50,11, 2, 49}; Program p=new Program(); p.SortArray(num); } public void SortArray(int[] numarray) { int swap = 0; for(int i=0; i<numarray.Length; i++) { for(int j=i+1; j<numarray.Length; j++) { if(numarray[i]<=numarray[j]) { swap=numarray[j]; numarray[j]=numarray[i]; numarray[i]=swap; } } Console.Write(numarray[i] + " "); } } } } |
Example 4: Write A Program To Sort One Dimensional Array In Desending Order Static Class Array Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; namespace Sort_Array_Des { public class Program { public static void Main(string[] args) { int[] num= {22,50,11, 2, 49}; Array.Sort(num); Array.Reverse(num); for(int i=0; i<num.Length; i++) { Console.Write(num[i] + " "); } } } } |
Example 5: Write A Program To Add The Diagonal Of Two-Dimensional Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System; namespace Add_Diagonal { public class Program { public static void Main(string[] args) { int[,] num= { {22,50,11, 2, 49}, {92,63,12,64,37}, {75,23,64,12,99}, {21,25,71,69,39}, {19,39,58,28,83}}; //Getting Row and Column Length int rowlength = num.GetLength(0); int columnlength = num.GetLength(1); int total=0; Console.Write("Diagonals are : "); for(int row=0; row<rowlength; row++) { for(int column=0; column<columnlength; column++) { if(row==column) { Console.Write(num[row,column] + " "); total += num[row,column]; } } } Console.WriteLine(": Sum : " + total); } } } |