Get Factorial in Windows Store App

In this article I explain how to get a Factorial in a Windows Store App.

In this article you can learn about Factorial Numbers. So first of all let us understand what a Factorial Number is. Then I will describe Factorial Numbers in programming terms.

Factorial Number

A number is a Factorial Number that is the product of a given positive integer multiplied by all lesser positive integers. The Symbol of Factorial is "!".

Example: Number 5!=5*4*3*2*1

Use the following procedure to create the Factorial app.

Step 1

First of all you have to create a New Windows Store Application.

  • 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 XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):

<Page

    x:Class="Factorial_app.MainPage"

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

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

    xmlns:local="using:Factorial_app"

    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="38*"/>

            <RowDefinition Height="23*"/>

            <RowDefinition Height="31*"/>

            <RowDefinition Height="32*"/>

            <RowDefinition Height="37*"/>

            <RowDefinition Height="607*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="282*"/>

            <ColumnDefinition Width="62*"/>

            <ColumnDefinition Width="339*"/>

        </Grid.ColumnDefinitions>

        <TextBlock Text="Get The Factorial Of Number" FontFamily="Arial" FontSize="15" FontWeight="ExtraBold" Foreground="Red" Grid.ColumnSpan="3" Grid.Row="1" TextAlignment="Center"  ></TextBlock>

        <TextBlock Text="Enter Number:" FontFamily="Arial" FontSize="15" FontWeight="ExtraBold" Foreground="Red" Grid.Column="1" Grid.Row="2"></TextBlock>

        <TextBox x:Name="Textbox1" Width="150" Height="32" VerticalAlignment="Top" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Left" Grid.RowSpan="2" />

        <TextBlock Text="Factorial is:" FontFamily="Arial" FontSize="15" FontWeight="ExtraBold" Foreground="Red" Grid.Column="1" Grid.Row="3"></TextBlock>

        <TextBlock x:Name="text2" FontFamily="Arial" FontSize="15" FontWeight="ExtraBold" Foreground="Red" Grid.Column="2" Grid.Row="3" Margin="5,0,661,0" Height="32" VerticalAlignment="Bottom" />

        <Button Content="Click" Click="Button1_click" Grid.Column="2" Grid.Row="4" FontSize="15" Foreground="Red" Background="Yellow" Width="137" Height="39" VerticalAlignment="Top" 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 Factorial_app

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        private void Button1_click(object sender, RoutedEventArgs e)

        {

            int num = Convert.ToInt32(Textbox1.Text);

            int fact = 1;

            for (int i = num; i > 0; i--)

            {

                fact = fact * i;

            }

            text2.Text = fact.ToString();

        }

    }

}

 

Step 4

Now Run Your app.

run-factorial-app.jpg

output-of-factorial-app.jpg