
Program Explanation: The program described above uses for loop for defining the conditions for primality. ") Ĭonsole.Write("The entered number is a Prime number. Example #2Ĭonsole.Write("Please enter the number to check for Primality: ") Ĭonsole.Write("The entered number is not a Prime number. Now we employed the conditional operator to print the number in the output console in case we received the number as a prime number. We use a Boolean parameter Prime for a flag in case we are receiving the value of a % b not equal to zero. The modulo operator returns 0 if a is perfectly divisible by b indicating the fact that b as a smaller natural number is a factor for the composite number a. The loop then passes through the range and uses the natural operation of modulo on the variable a by the divisor b. The program above uses a lower limit of Natural number i.e.2 defining ‘a’ as a natural number in the range of 2 ranging to 99 with the post-operation increment of 1, the next step uses a variable ‘b’ with the similar range but bound to a condition in its upper limit is always less than ‘a’. Program Explanation: The above program is a classic example of the use of loops and conditional operators for determining the prime numbers in a fixed range of numbers. If (a != b & a % b = 0) //modulo operators employedĬonsole.Write("\t" + a) //printing the correct variableĬonsole.ReadKey() //hold the output screen Static void Main(string args) // this function defines the entry pointĬonsole.WriteLine("Prime Numbers between 1 to 100 : ") įor (int a = 2 a <= 100 a++) //upper limit and lower limit are definedįor (int b = 2 b <= 100 b++)// base logic for the primality

Example #1Ĭ# program to print the list of all prime numbers between 1 to 100.



Here is JavaScript code to generate a list of an arbitrarily large number of prime numbers.īecause this is JavaScript, you can even open up your browser's JavaScript console and run this code for yourself.Let us try to conceptualize prime numbers with the following programming examples. How to Create a List of Primes Using the Sieve of Eratosthenes An illustration of how the Sieve of Eratosthenes Works step-by-step. I assembled this list for my own uses as a programmer, and wanted to share it with you.īefore I show you the list, here's how to generate a list of prime numbers of your own using a few popular languages. Here's a list of all 2,262 prime numbers between zero and 20,000.
