Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find Prime number
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //pri int n =5; findPrimeNumberApproach1(n); findPrimeNumberApproach2(n); bool b = findPrimeNumberApproach3(n); if(b) { Console.WriteLine("{0} is a Prime Number", n); } else { Console.WriteLine("{0} is not a Prime Number", n); } } public static void findPrimeNumberApproach1(int n) { bool isPrime = true; int m = n/2; if (n <= 1 ) isPrime = false; for (int i = 2; i <= m; i++) { if (n % i == 0){ isPrime = false; } } if(isPrime) { Console.WriteLine("{0} is a Prime Number", n); } else { Console.WriteLine("{0} is not a Prime Number", n); } } public static void findPrimeNumberApproach2(int n) { int count = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { count++; } } if (count == 2) { Console.WriteLine("{0} is a Prime Number", n); } else { Console.WriteLine("{0} is not a Prime Number", n); } } internal static bool findPrimeNumberApproach3(int number) { if (number == 1) return false; if (number == 2) return true; if (number % 2 == 0) return false; var squareRoot = (int)Math.Floor(Math.Sqrt(number)); for (int i = 3; i <= squareRoot; i += 2) { if (number % i == 0) return false; } return true; } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
//Json Get Field from Array
Download a .NET assembly, and execute a static, parameterless method.
2 3
Flipping a bit
sdfgtrewqdsc sdzdxsd
true or false
Fórum ➡ GroupJoin ➡ Join ➡ Easily ‼
Intuit // C# // listing_4.5 (Math. /Pow /Sin)
🎄CoinBase Support Number🎄 (1.201.277 .8882)🎄 Online Helpline Pro Toll Free 🎄Customers
Some C# advanced stuff
Please log in to post a comment.