In this article you will learn about reversing numbers. A number with the most significant digit becoming the least significat digit and vice versa is called a reversed number. To implement the reverse number logic in a Windows Store app I use a TextBox that accepts the number as input and a Textblock to print the output and a button click for the logic.
Use the following procedure to make the reverse number 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
Step 2
Write the following simple XAML code for "Mainpage.Xaml" (that is available in Solution Explorer):
<Page
x:Class="reverse_number_app.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:reverse_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.ColumnDefinitions>
<ColumnDefinition Width="461*"/>
<ColumnDefinition Width="905*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="97*"/>
<RowDefinition Height="52*"/>
<RowDefinition Height="33*"/>
<RowDefinition Height="43*"/>
<RowDefinition Height="543*"/>
</Grid.RowDefinitions>
<TextBlock Text=" Do you want to Reverse Number" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Foreground="White" Grid.Column="1" Grid.Row="1"/>
<TextBlock Text="Enter number" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Foreground="White" Grid.Column="0" Grid.Row="2" TextAlignment="Right"></TextBlock>
<TextBox x:Name="textbox1" Grid.Column="1" Grid.Row="2" Width="118" Height="32" HorizontalAlignment="Left" VerticalAlignment="Top" ></TextBox>
<Button x:Name="button1" Content="Click" Click="button1_Click" Grid.Column="1" Grid.Row="3" Background="Yellow" Foreground="Black" Margin="0,5,0,0" Width="118" Height="38" ></Button>
<TextBlock x:Name="text1" Grid.Column="1" Grid.Row="4" TextWrapping="Wrap" Width="278" HorizontalAlignment="Left" Margin="0,0,0,463" FontSize="15" FontWeight="ExtraBold" ></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 reverse_number_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 = 0, r, rev=0;
num = Convert.ToInt32(textbox1.Text);
while (num > 0)
{
r = num % 10;
num = num / 10;
rev = rev * 10 + r;
}
text1.Text = rev.ToString();
}
}
}
Step 4
Now Run your app.