In this article I explain how to separate each digit of a number and then add each digit and show the sum of each digit. So I will first seperate each digit, then add them.
Use the following procedure to do that.
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="sum_of_each_digit_entered_number.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:sum_of_each_digit_entered_number"
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="122*"/>
<RowDefinition Height="39*"/>
<RowDefinition Height="33*"/>
<RowDefinition Height="37*"/>
<RowDefinition Height="537*"/>
</Grid.RowDefinitions>
<TextBlock Text="Sum of Each Digit of Entered 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="140" Height="32" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Button x:Name="button1" Content="Click" Click="button1_Click" Grid.Column="1" Grid.Row="3" Background="Yellow" Foreground="Black" Width="118" Height="38" VerticalAlignment="Top" Grid.RowSpan="2" />
<TextBlock x:Name="text1" Grid.Column="1" Grid.Row="4" TextWrapping="Wrap" Width="219" HorizontalAlignment="Left" Margin="0,6,0,497" FontSize="15" FontWeight="ExtraBold" />
</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 sum_of_each_digit_entered_number
{
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, sum = 0;
num = Convert.ToInt32(textbox1.Text);
while (num > 0)
{
r = num % 10;
num = num / 10;
sum = sum + r;
}
text1.Text = sum.ToString();
}
}
}
Step 4
Now Run your app.