Wednesday, 10 December 2014

Number Pattern in C# - 15 (Pascal Triangle)

Ques: Write a program to print PASCAL triangle



In pascal triangle we start with print "1" at the top, and after that continue placing numbers below it in the for pyramid triangle. Each number in this triangle is the sum of above two number starting from left.

Suppose we have to print this pascal triangle
    
      1                        
      1  1                      
    1  2  1                     
  1  3  3  1           
1  4  6  4  1                

ROW 0 = 1;
ROW 1 = ( 0 + 1);
ROW 2 = ( 0 + 1)( 1 + 0);
ROW 3 = ( 0 + 1 )( 1 + 2 )( 2 + 1 )( 1 + 0 );
ROW 4 = ( 0 + 1 )( 1 + 3 )( 3 + 3 )( 3 + 1 )( 1 + 0 );


There are lots of way to create Pascal Triangle Two of them are here

Source Code 1:

  1. using System;  
  2. public class PascleTriangle  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row,i,j,c;  
  7.         Console.Write("Enter the no. of row you want to print: ");  
  8.         row=Convert.ToInt32(Console.ReadLine());  
  9.     
  10.         for(i=0;i<=row;i++)  
  11.         {  
  12.             c=1;  
  13.             for(j=i;  j<=row-1; j++)  
  14.             Console.Write(" ");  
  15.           
  16.             for(j=0;j<=i;j++)  
  17.             {  
  18.                 Console.Write("{0} ",c);  
  19.                 c=(c*(i-j)/(j+1));  
  20.             }  
  21.             Console.WriteLine();      
  22.         }  
  23.         Console.ReadLine();  
  24.     }  
  25. }

OR

Source Code 2:

  1. using System;  
  2. public class PascleTriangle  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int i,j,row,pas;  
  7.           
  8.         Console.Write("Enter the no. of rows in pascle Triangle: ");  
  9.         row=Convert.ToInt32(Console.ReadLine());  
  10.   
  11.         for(i=0; i<=row;i++)  
  12.         {  
  13.             for(j=0;j<=(row-i);j++)  
  14.             Console.Write(" ");  
  15.   
  16.             for(j=0; j<=i; j++)  
  17.             {  
  18.                 pas=calc(i)/(calc(j)*calc(i-j));  
  19.                 Console.Write("{0} ",pas);  
  20.             }  
  21.             Console.WriteLine();  
  22.         }  
  23.         Console.ReadLine();  
  24.     }  
  25.   
  26.     public int calc(int num)  
  27.     {  
  28.         int x, res=1;  
  29.         for(x=1;x<=num;x++)  
  30.         res=res*x;  
  31.         return res;  
  32.     }  
  33. }  

  
Screen Shot of Pascal triangle
Fig: Screenshot of Output



Tuesday, 9 December 2014

Application of Reverse number

Ques: Write a program where user gives two inputs digit , the program  reverse the inputs and add them and again reverse there sum.

  For Exp: 

Input:
Enter first number: 56
Enter second number:89

Output:
reverse of first number: 65
reverse of second number:98

sum of two reverse no is: 163

reverse of sum: 361


Source Code:
  1. using System;  
  2.  
  3. public class SumOfDigit   
  4. {  
  5.     public static void Main()  
  6.     {  
  7.         int number1, number2;  
  8.         int sum=0;  
  9.   
  10.         Console.Write("Enter the first Number: ");  
  11.         number1=Convert.ToInt32( Console.ReadLine() );  
  12.   
  13.         Console.WriteLine("Reverse of first Number: {0}\n", ReverseNumber( number1 ));        
  14.   
  15.         Console.Write("Enter the second Number: ");  
  16.         number2=Convert.ToInt32( Console.ReadLine() );  
  17.   
  18.         Console.WriteLine("Reverse of second Number: {0}\n", ReverseNumber( number2 ));  
  19.   
  20.         sum = ReverseNumber( number1 )+ReverseNumber( number2 );  
  21.   
  22.         Console.WriteLine("\n\nSum of Two reverse number is: \t{0}", sum);  
  23.   
  24.         sum = ReverseNumber( (ReverseNumber( number1 )) + (ReverseNumber( number2 )) );  
  25.         Console.WriteLine("Reverse of sum of Two number is: \t{0}",sum);  
  26.     }  

  27.     public static int ReverseNumber( int digit )  
  28.  
  29.     {  
  30.         int x,reverse=0;  
  31.           
  32.         while(digit !=0)  
  33.         {  
  34.             x=digit%10;  
  35.             reverse=reverse*10+x;  
  36.             digit=digit/10;  
  37.         }  
  38.         return (reverse);  
  39.     }  
output of application of reverse a number
Fig: Screenshot of Output



Note: we can also optimize the code by adding a variable reverseSum and store the value of reverse of sum (Line-29) in it.







Star Pattern in C# - 7

Ques: Wap a program to print the given star pattern

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

Source Code:


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

  • screenshot of star pattern program in c-sharp
    Fig: Screenshot of Output

    Note: We can use the logic of print second pattern in Line - 29 in both the Outer for loop.