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.


    Thursday 27 November 2014

    Application of Random Class: CRAPS

    CRAP is a dice game played in casino and back alleys throughout the world.

    A Game of Chance CRAP
    Rules of the game:
    You roll two dice. Each die has a six faces, which contain one, two, three, four, five, six spots, respectively. After the dice have come to rest, the some of spots n the two upward face is calculated.
    if some is 7 or 11 on the first throw, you win.
    if some is  2,3 or 12 on the first throw (called CRAPS ) you lose (i.e the house wins).
    if some is 4,5,6,8,9 or 10 on the first throw, that sum become "your point".
             To win you must continues rolling your dice until you "make your point" (i.e roll that same value).
    You lose by rolling a 7 before making your point.
    Source code:
    // class crap.cs 
    1. using System;
    2. public class CRAP
    3. {
    4. //Creating a random number generator for use in methd RollDice
    5. private Random randomNumber = new Random();
    6. private enum Status //ENUM WITH CONSTATNT THAT REPRESENT GAME STATUS
    7. {
    8. CONTINUE,
    9. WON,
    10. LOST
    11. }
    12. private enum DiceNames
    13. {
    14. SNAKE_EYES = 2,
    15. TREY = 3,
    16. SEVEN = 7,
    17. YO_LEVEN = 11,
    18. BOX_CARS = 12
    19. }
    20. // PLAY ONE GAME OF CRAP
    21. public void Play()
    22. {
    23. Status gameStatus = Status.CONTINUE;
    24. int myPoint = 0;
    25. int sumOfDice = RollDice();
    26. switch ((DiceNames)sumOfDice)
    27. {
    28. case DiceNames.SEVEN:
    29. case DiceNames.YO_LEVEN:
    30. gameStatus = Status.WON;
    31. break;
    32. case DiceNames.BOX_CARS:
    33. case DiceNames.SNAKE_EYES:
    34. case DiceNames.TREY:
    35. gameStatus = Status.LOST;
    36. break;
    37. default:
    38. gameStatus= Status.CONTINUE;
    39. myPoint = sumOfDice;
    40. Console.WriteLine("Point is {0}", myPoint);
    41. break;
    42. }
    43. while (gameStatus == Status.CONTINUE)
    44. {
    45. sumOfDice = RollDice(); // roll dice again
    46. if (sumOfDice == myPoint)
    47. gameStatus = Status.WON;
    48. if (sumOfDice == (int)DiceNames.SEVEN)
    49. gameStatus = Status.LOST;
    50. }// end of while method
    51. // Display won or loss
    52. if (gameStatus == Status.WON)
    53. Console.WriteLine("Palyer Wins");
    54. else
    55. Console.WriteLine("Player Losses");
    56. }//end of method Play
    57. public int RollDice()
    58. {
    59. int die1 = randomNumber.Next(1, 7);
    60. int die2 = randomNumber.Next(1, 7);
    61. int sum = die1 + die2;
    62. //Display result of this roll
    63. Console.WriteLine("Player rolled {0} + {1} = {2}", die1, die2, sum);
    64. return sum;
    65. }// End method roll dice
    66. }
    //class CrapTest.cs 

    1. using System;
    2. class CrapTets
    3. {
    4. public static void Main()
    5. {
    6. CRAP game = new CRAP();
    7. game.Play();
    8. Console.ReadLine();
    9. }
    10. }

    Screen Shots of Output: 


    Dice game in C-Sharp.Application of Random Class
    Fig 1: Snapshot of output



    Dice game in C-Sharp.Application of Random Class
    Fig 2: Snapshot of output



    Dice game in C-Sharp.Application of Random Class
    Fig 3: Snapshot of output


    Dice game in C-Sharp.Application of Random Class
    Fig 4: Snapshot of output