Display Number From 1 to 100 Without Loop in Windows Store App

Introduction

Today I am describing a very simple but interesting thing. Because we always print number from 1 to 100 using a loop. But someone asked me how to print these numbers without using a loop. Really at that time I had no idea although I proceeded to do it. I finally got the idea to use recursion (in which a method calls itself) and print the number.

Use the following procedure to implement it.

Step 1

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

  • Open Visual Studio 2012
  • "File" -> "New" -> "Project..."
  • Choose "Template" -> "Visual C#" -> "Window Store app"
  • Choose "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="number_app.MainPage"

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

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

    xmlns:local="using:number_app"

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

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

    mc:Ignorable="d">

 

    <Grid Background="Red">

        <Grid.RowDefinitions>

            <RowDefinition Height="116*"/>

            <RowDefinition Height="45*"/>

            <RowDefinition Height="518*"/>

            <RowDefinition Height="89*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="215*"/>

            <ColumnDefinition Width="479*"/>           

            <ColumnDefinition Width="200*"/>

        </Grid.ColumnDefinitions>

        <TextBlock Text="Display Number from 1 to 100 without loop" FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2"></TextBlock>

        <TextBlock x:Name="text1"  FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" Width="722" TextWrapping="Wrap" Margin="0,0,0,40"></TextBlock>       

    </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 number_app

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }      

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

            number(1);

        }          

        public void number(int num)

        {

            if(num <=100)

            {

            text1.Text += num.ToString() + " ";

            number(num + 1);

            }

        }

    }

}

 

Step 4

Run your app and see the output.

ouput-of-recursive-app.jpg


Similar Articles