Print Pascal's Triangle in Windows Store App

In this article I will describes how to print a Pascal's Triangle in a Windows Store app.

In this article I am explaining how to print a Pascal's Triangle in a Windows Store app. Pascal's Triangles are famous in mathematics and the C language but here I will create them in a Windows Store app. So let us first of all understand Pascal's Triangles.

Pascal's Triangles

They start with "1" at the top, then numbers continue to be placed below previous numbers in a triangular pattern. Each number is just the two numbers above it added together (except for the edges, which are all "1").

Use the following procedure to create a Pascal's Triangle in a Windows Store app.

Step 1

First of all you have to create a new Windows Store Application, as in the following:

  • Open Visual Studio 2012
  • "File" -> "New" -> "Project..."
  • Choose Template: "Visual C#" -> "Window Store app"
  • "Blank App (XAML)", then rename the application

new-windows-store-app.jpg

Step 2

Write the following simple XAML code for "Mainpage.Xaml" (that is available in Solution Explorer):

<Page

    x:Class="pascal_triangle.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:pascal_triangle"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="Blue">

        <Grid.RowDefinitions>

            <RowDefinition Height="102*"/>

            <RowDefinition Height="29*"/>

            <RowDefinition Height="36*"/>

            <RowDefinition Height="34*"/>

            <RowDefinition Height="231*"/>

            <RowDefinition Height="21*"/>

            <RowDefinition Height="27*"/>

            <RowDefinition Height="288*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="189*"/>

            <ColumnDefinition Width="494*"/>

        </Grid.ColumnDefinitions>

        <TextBlock Text="Print Pascal's Triangles" FontFamily="Arial" FontSize="15" FontWeight="ExtraBold" Foreground="Red" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Height="17"></TextBlock>

        <TextBlock Text="Enter row no" Grid.Column="0" Grid.Row="2" FontSize="15" FontFamily=" Arial" Foreground="Red" HorizontalAlignment="Right" FontWeight="ExtraBold"></TextBlock>

        <TextBox x:Name="textbox1" Grid.Column="1" Grid.Row="2" Width="135" HorizontalAlignment="Left" VerticalAlignment="Top" Height="32" ></TextBox>

        <TextBlock x:Name="text1" FontSize="15" Foreground="Red" Grid.Column="1" Grid.Row="4" FontWeight="ExtraBold" Margin="0,0,655,0" VerticalAlignment="Top" Height="225" TextWrapping="Wrap" />

        <Button x:Name="button1" Content="Click" Click="Button1_Click" Background="Yellow" Foreground="Red" Height="37" Width="122" VerticalAlignment="Top" Grid.Row="2" HorizontalAlignment="Left"  Grid.Column="1" Margin="0,33,0,0" Grid.RowSpan="2"/>  

    </Grid>

</Page>

 

Step 3

Now write the following C# code for the button within "Mainpage.Xaml.cs":

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace pascal_triangle

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        private void Button1_Click(object sender, RoutedEventArgs e)

        {

            text1.Text = "";

            int row = Convert.ToInt32(textbox1.Text);

           

            for (int i = 0; i <row ; i++)

            {

                int c = 1;

                for ( int j = 0; j <row - i; j++)

                {

                    text1.Text += " ";

                }

                for (int k=0;k <= i; k++)

                {

                    text1.Text += c.ToString();

                    c = c * (i - k) / (k + 1);                

                }

                text1.Text += Environment.NewLine;

                text1.Text += Environment.NewLine;

            }

            text1.Text += Environment.NewLine;

        }

 

        }

    }

 

Step 4

Run your App.

run-pascal-app.jpg


Output-of-pascal-app.jpg