Saturday 31 January 2015

Number Pattern in C# - 18

Question: Program to print the following pattern.


9
0 1
2 3 4
5 6 7 8
9 0 1 2 3


Code Snippet:

  1. using System;  
  2. public class pattern  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col, i, n, x=0;  
  7.   
  8.         Console.Write("Enter the no. of row: ");  
  9.         n=Convert.ToInt32(Console.ReadLine());  
  10.         Console.WriteLine("9");  
  11.   
  12.         for(row=2; row<=n; row++)  
  13.         {  
  14.             for(col = 1; col<=row; col++,x++)  
  15.             {  
  16.                 if(x<10)  
  17.                 Console.Write("{0} ",x);  
  18.   
  19.                 else  
  20.                 {  
  21.                     for(i=0; i<=3; i++)  
  22.                     Console.Write("{0} ",i);  
  23.                     break;  
  24.                 }  
  25.                       
  26.             }     
  27.             Console.WriteLine();  
  28.         }  
  29.             Console.ReadLine();  
  30.     }  
  31. }  


Output of pattern programming
Fig: Screenshots of Output


Number Pattern in C# - 17

Question: Program to print the following pattern?


          1
         1 2 1
      1 2 3 2 1
   1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
   1 2 3 4 3 2 1
      1 2 3 2 1
         1 2 1
            1


Code Snippet:


  1. using System;  
  2. public class Pattern  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col, n, temp;  
  7.   
  8.         Console.Write("Enter the no. of row: ");  
  9.         n = Convert.ToInt32(Console.ReadLine());  
  10.         temp =n;  
  11.   
  12.         for(row = 1; row <=n; row++)  
  13.         {  
  14.             for(col = 1; col <temp; col++)  
  15.             Console.Write("  ");  
  16.             temp--;  
  17.   
  18.             for(col = 1; col < row; col++)  
  19.             Console.Write("{0} ", col);  
  20.   
  21.             for(col= row; col>=1; col--)  
  22.             Console.Write("{0} ", col);  
  23.               
  24.             Console.WriteLine();  
  25.         }  
  26.   
  27.         for(row = 1; row<=n; row++)  
  28.         {  
  29.             for(col = 1; col<=row; col++)  
  30.             Console.Write("  ");  
  31.   
  32.             for(col = 1; col<=n-row; col++)  
  33.             Console.Write("{0} ", col);  
  34.   
  35.             for(col = n-row-1; col>=1; col--)  
  36.             Console.Write("{0} ", col);  
  37.   
  38.             Console.WriteLine();  
  39.         }  
  40.   
  41.             Console.ReadLine();  
  42.     }  
  43. }  


Output of pattern programming
Fig: Screenshot of Output



Friday 30 January 2015

Number Pattern in C# - 16

Question: Program to print the following pattern?


           1
          2 1 2
       3 2 1 2 3
    4 3 2 1 2 3 4
 5 4 3 2 1 2 3 4 5
    4 3 2 1 2 3 4
       3 2 1 2 3
          2 1 2
             1


Code Snippet:


  1. using System;  
  2. public class Pattern  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col, n, temp;  
  7.   
  8.         Console.Write("Enter the no. of row: ");  
  9.         n = Convert.ToInt32(Console.ReadLine());  
  10.   
  11.         temp = n;  
  12.   
  13.         for(row=1; row<=n; row++)  
  14.         {  
  15.             for(col = 1; col<temp; col++)  
  16.             Console.Write("  ");  
  17.             temp--;  
  18.   
  19.             for(col = row; col >=1; col--)  
  20.             Console.Write("{0} ",col);  
  21.       
  22.             for(col = 2; col<=row; col++)  
  23.             Console.Write("{0} ",col);  
  24.   
  25.             Console.WriteLine();  
  26.         }  
  27.   
  28.         for(row = 1; row<=n; row++)  
  29.         {  
  30.             for(col = 1; col <= row; col++)  
  31.             Console.Write("  ");  
  32.   
  33.             for(col = n-row; col >=1; col--)  
  34.             Console.Write("{0} ", col);  
  35.   
  36.             for(col = 2; col<=n-row; col++)  
  37.             Console.Write("{0} ", col);  
  38.   
  39.             Console.WriteLine();  
  40.         }  
  41.   
  42.         Console.ReadLine();  
  43.     }  
  44. }  


Output of numberpattern
Fig: Screenshot of output


Tuesday 27 January 2015

Arrays using Command - Line Arguments

Introduction :

In many system it is possible to pass argument from the command line (known as command line argument) to an application by including a parameter of type string[] (i.e. an array of string) in the parameter list of Main. We named this parameter args. When an application is executed from the the command prompt, the execution environment the command-line argument that appear after the application name the application's main method as string in the one dimensional array args. The Number of argument passed from the command line is obtained from the accessing the arrays length property. For example the command  "MyApplication a b"  passes two command line argument to application MyApplication. Note that command line argument separated by white space not commas.

Example

  1. using System;  
  2. public class InitArray  
  3. {  
  4.     public static void Main(string[] args)  
  5.     {  
  6.         if(args.Length != 3)  
  7.             Console.WriteLine("Please re-enter the entire command including\n An array size, Initial Value and Increament");  
  8.   
  9.         else  
  10.         {  
  11.             int arrayLength = Convert.ToInt32( args[0] );  
  12.             int[] array = new int[ arrayLength ];  
  13.   
  14.             int initialValue = Convert.ToInt32( args[1] );  
  15.             int increament = Convert.ToInt32( args[2] );  
  16.   
  17.             forint counter = 0; counter<array.Length; counter++ )  
  18.                 array[counter] = initialValue + increament * counter;  
  19.   
  20.             Console.WriteLine( "{0}{1,8}""Index""Value" );  
  21.   
  22.             for(int counter = 0; counter < array.Length; counter++)  
  23.   
  24.             Console.WriteLine("{0,3}{1,8}", counter, array[counter]);  
  25.         }  
  26.     }  
  27. }  


In the above example it takes three command line arguments to initialize the array. First is the length of the array Second is the initial value of the array third is the Increment between values in the array.


Output of Command line argument
Fig: Screenshot of output


In this Output the size of array is 5 the initial value of array is 0 and there is increment of 8.



Variable-Length Argument Lists (VARARGS)

Introduction:

Variable - length argument lists allow you to create methods that receive an arbitrary number of arguments. A one-dimensional array-type argument preceded by the keyword params in a method's parameter list indicates that the method receives a variable number of argument with the type of array elements. This use of params modifiers can occurs only in the last entry of the parameter list. While you can use method overloading and arraypassing to accomplish much of what is accomplished with "varargs"- another name for variable length argument lists-using the param modifiers is more concise.

Example:

  1. using System;  
  2. public class VarargsTest  
  3. {  
  4.     public static double Average(params double[] numbers)  
  5.     {  
  6.         double total = 0.0;  
  7.         foreach(double d in numbers)  
  8.             total += d;  
  9.   
  10.         return total/numbers.Length;  
  11.     }  
  12.   
  13.     public static void Main()  
  14.     {  
  15.         double d1 = 10.0;  
  16.         double d2 = 12.5;  
  17.         double d3 = 15.6;  
  18.         double d4 = 14.8;  
  19.   
  20.         Console.WriteLine( "d1 = {0:F2}\nd2 = {1:F2}\nd3 = {2:F2}\nd4 = {3:F2}", d1, d2, d3, d4 );  
  21.   
  22.         Console.WriteLine("Average of d1 and d2 is {0:F2}", Average(d1, d2));  
  23.         Console.WriteLine("Average of d1, d2 and d3 is {0:F2}", Average(d1, d2, d3));  
  24.         Console.WriteLine("Average of d1, d2, d3 and d4 is {0:F2}", Average(d1, d2, d3, d4));  
  25.   
  26.         Console.ReadLine();  
  27.     }  
  28. }  


Output of varargs
Fig: Screenshot of Output