

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. Let us try to conceptualize prime numbers with the following programming examples.
