Wednesday 11 February 2015

Star Pattern - 8

Question: Program to print the following Star Pattern.

* * * * * * * * *
   * * * * * * *
      * * * * *
         * * *
            *
         * * *
      * * * * *
   * * * * * * *
* * * * * * * * *

Code Snippet:

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

Output of Program
Fig: Screenshot of output



Tuesday 10 February 2015

Number Pattern In C# - 27

Question: Program to print following pattern.

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

Code Snippet:

using System; public class Pattern { public static void Main() { int i, j, n, temp, count; Console.Write("Enter the no. of row: "); n=Convert.ToInt32(Console.ReadLine()); temp=n; count=n; for(i=n; i>=1; i--) { for(j=1; j<temp; j++) Console.Write(" "); for(j=i; j<=count; j++) Console.Write("{0} ",j); for(j=count-1; j>=i; j--) Console.Write("{0} ",j); temp--; Console.WriteLine(); } Console.ReadLine(); } }


Screenshot of program
Fig: Screenshot of output



Sunday 8 February 2015

Number pattern in C# - 26

Question: Program to print the binary Right triangle.

1
1 0
1 0 1
1 0 1 0
1 0 1 0 1

Code Snippet:


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

Output of number pattern
Fig: Screenshot of Output

Saturday 7 February 2015

Number Pattern in C# - 25

Question: Program to print the following Number Pattern?


01
02 04
03 06 09
04 08 12 16
05 10 15 20 25

Code Snippet:


  1. using System;  
  2. public class Pattern  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col, n, pr;  
  7.         Console.Write("Enter the no. of row: ");  
  8.         n = Convert.ToInt32(Console.ReadLine());  
  9.   
  10.         for(row = 1; row <= n; row++)  
  11.         {  
  12.             pr = 0;  
  13.             for(col = 1; col <= row; col++)  
  14.             {     
  15.                 pr = row*col;  
  16.       
  17.                 Console.Write("{0:D2} ", pr); // convert 1 to 01
  18.             }  
  19.             Console.WriteLine();  
  20.         }  
  21.             Console.ReadLine();  
  22.     }  
  23. }  

Output of number pattern in c-sharp.
Fig: Screenshot of output



Sunday 1 February 2015

Number Pattern in C# - 24

Question: Program to print the following pattern.


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

Code Snippet:

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

Output of Number Pattern
Fig: Screenshot of output


Number Pattern in C# - 23

Question: Program to print the following Pattern.


9
8 9 8
7 8 9 8 7
6 7 8 9 8 7 6

Code Snippet:

  1. using System;  
  2. public class Pattern  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col, n, temp=9,count;  
  7.         Console.Write("Enter the no. of row: ");  
  8.         n=Convert.ToInt32(Console.ReadLine());  
  9.         count=temp;  
  10.   
  11.         for(row = 1; row<=n; row++)  
  12.         {  
  13.             for(col = count; col<=temp; col++)  
  14.                 Console.Write("{0} ",col);  
  15.             for(col=temp-1; col>=count; col--)  
  16.                 Console.Write("{0} ",col);  
  17.   
  18.             count--;  
  19.   
  20.             Console.WriteLine();  
  21.         }  
  22.   
  23.             Console.ReadLine();  
  24.     }  
  25. }  


Output of number pattern
Fig: Screenshot of output

Number Pattern in C# - 22

Question: Program to print the following Pattern.

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

Code Snippet:

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

Output of number pattern
Fig: Screenshot of the output