Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sayantan1413 committed Nov 28, 2020
1 parent 84a0d3f commit 4075818
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions C-Sharp/math/Prime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//This is a problem of taking a number as a input
//and then display the number is prime or not.
// e.g :
// Input :
// Enter a number : 13
// Output :
// 13 is a Prime Number.
using System;

namespace primenumber
{
class prime
{
//This function isPrime checks whether the number is prime or not.
//This function takes the number as a parameter.
//Then it checks if the number is divisible by any other number other
//than 1 or itself it returns a false value else a true value.
static bool isPrime(int number)
{
//handles the case if the entered number is equal to 1 or less than that
if (number <= 1)
{
return false;
}
else
{
//The iteration starts from the smallest prime number
for (int iteration = 2; iteration < number; iteration++)
{
//Checks if the number is divisible by any other number other than 1 or itself
if (number % iteration == 0)
{
return false;
}
}
return true;
}
}
//The value from the isPrime comes to this main function and display accordingly
static void Main(string[] args)
{
{
int number;
bool prime;
Console.WriteLine( "Input :" );
Console.Write( "Enter a number :" + " " );
//Reads the input from the user
number = int.Parse(Console.ReadLine());
//The value from the isPrime either true or false is stored in the variable prime
prime = isPrime(number);
Console.WriteLine( "Output :" );
//True condition
if (prime)
{
Console.WriteLine( number + " is prime number" );
}
//False condition
else
{
Console.WriteLine( number + " is not prime number" );
}
}
}
}
}

0 comments on commit 4075818

Please sign in to comment.