Subtract Two Number Without Using Subtraction Operator in Windows Store App

Introduction

In my previous article I explained about Sum of Two number without using addition operator and in this article I will describe how to subtract two number without using a subtraction operator. So let us understand the algorithms of it.

In C# "~" is called 1's complement operator; it works as described here.

We all know that "-num1=(~num1 +1)".

So:

~num1= -num - 1

sum=num + ~num1 +1

sum =num + (-num1 -1) +1

sum=num -num1 -1 +1

sum=num -num1

Use the following procedure to create it.

Step 1

First of all you must create a new Windows Store Application.

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="subtract_app.MainPage"

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

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

    xmlns:local="using:subtract_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="233*"/>

            <ColumnDefinition Width="142*"/>

            <ColumnDefinition Width="204*"/>

            <ColumnDefinition Width="787*"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="78*"/>

            <RowDefinition Height="49*"/>

            <RowDefinition Height="43*"/>

            <RowDefinition Height="40*"/>

            <RowDefinition Height="38*"/>

            <RowDefinition Height="520*"/>

        </Grid.RowDefinitions>

        <TextBlock Text="Subtract Two number without Using subtraction Operator" Foreground="White" FontSize="20" FontWeight="ExtraBold" Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="2"></TextBlock>

        <TextBlock Text="Enter Number1" Foreground="White" FontSize="15" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="2"></TextBlock>

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

        <TextBlock Text="Enter Number2" Foreground="White" FontSize="15" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="3"></TextBlock>

        <Button x:Name="button1" Content="Click" Click="Button1_Click" Grid.Column="2" Grid.Row="4" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Yellow" Foreground="Black" Width="120" Height="38" />

        <TextBox x:Name="textbox2" Grid.Column="2" Grid.Row="3" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Height="32" ></TextBox>

        <TextBlock x:Name="text1" Foreground="White" FontSize="15" FontWeight="ExtraBold" Grid.Column="3" Grid.Row="3" Width="200" HorizontalAlignment="Left" ></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 subtract_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 num1 = 0, num2 = 0;

            int sum = 0;

            num1 = Convert.ToInt32(textbox1.Text);

            num2 = Convert.ToInt32(textbox2.Text);         

            sum = num1 + ~num2 + 1;

            text1.Text = sum.ToString();

        }

    }

}

 

Step 4

Run your app.

subtract-app-output.jpg


Similar Articles