

Program Explanation: The program above illustrates the process of determining the primality of a number using while loop in C#.

N = Convert.ToInt32(Console.ReadLine()) // read the user inputĬonsole.WriteLine(n + " Prime Number as there are no factors") Ĭonsole.WriteLine(n + " not a Prime Number ") The input read field captures the user input an assigns to the variable n, the counter parses from the value of 2 to a value of n-1 and tests the conditions of divisibility to determine the number is prime or not.the additional feature involved in the program employs the use of a variable m with value of n/2 or exactly half of the initial user input, the program parses the loop over only up to a value of m. 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. Let us try to conceptualize prime numbers with the following programming examples.
