In this article I explain how to determine if a specific number is a prime number or not. We all know that a number that can only be divided by one or itself is called a prime number. But implementing in a program the determination of whether a number is prime is not simple. That is why I have written some simple logic for determining prime numbers in an app.
Use the following procedure to create the prime number finder app.
Step 1
First of all you have to create a New Window Store Application, as in:
- Open Visual Studio 2012
- "File" -> "New" -> "Project..."
- Choose Template: "Visual C#" -> "Window Store app"
- "Blank App (XAML)" then rename the application.
Step 2
Now the project will be opened; go into the Solution Explorer and choose "MainPage.Xaml" designing and "MainPage.Xaml.cs" to write the business logic.
Step 3
Now I have written the simple XAML code In "Mainpage.Xaml"; see:
<Page
x:Class="prime_app.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:prime_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="46*"/>
<RowDefinition Height="44*"/>
<RowDefinition Height="37*"/>
<RowDefinition Height="48*"/>
<RowDefinition Height="593*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="79*"/>
<ColumnDefinition Width="188*"/>
<ColumnDefinition Width="416*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Check wether Number is Prime or not" FontFamily="Arial" FontSize="15" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="0" Foreground="Red" ></TextBlock>
<TextBlock Text="Enter Number:" FontFamily="Arial" FontSize="15" FontWeight="ExtraBold" Foreground="Red" Grid.Column="1" Grid.Row="1" ></TextBlock>
<TextBox x:Name="textbox1" Grid.Column="1" Grid.Row="1" Width="200" Height="32" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="166,0,0,0"/>
<TextBlock Text="Number is:" FontFamily="Arial" FontSize="15" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="2" Foreground="Red" ></TextBlock>
<TextBlock x:Name="text2" FontSize="15" FontFamily="Arial" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="2" Foreground="Red" Margin="166,0,60,0" RenderTransformOrigin="-1.369,0.135" />
<Button x:Name="Button1" Grid.Column="1" Grid.Row="3" Background="Yellow" Foreground="Red" Width="151" Height="43" Content="Click" FontSize="20" FontWeight="ExtraBold" Margin="165,0,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.266,-0.119" Click="Button1_Click" />
</Grid>
</Page>
Step 4
Write the C# code in the "Mainpage.Xaml.cs" page; see:
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 prime_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);
for (int i = 2; i <= num - 1; i++)
{
if (num % i == 0)
{
text2.Text = "Not a Prime Number";
break;
}
if (i == num - 1)
{
text2.Text = "Prime Number";
}
}
if (num == 1 || num == 2)
{
text2.Text = "Prime Number";
}
}
}
}
Step 5
Run your application.
Step 6
Now enter a number in the TextBox to determine if the number is prime or not.