## Prime Number Lister

An enhancement of the Prime Number Checker, that will hopefully demonstrate how useful methods are when you can call them over and over again:

Ask the user for the starting and stopping points for the Prime Number Lister (this will be the range of numbers we check for primes).
Loop through every number between the two user-provided numbers, and determine which ones are prime. Display every prime number in the given range. |

*Try not to look at the answers until you’ve solved the problem. A*

*lso note that this code has not been error proofed and thus any input that is not a positive integer could cause it to crash or misbehave.*

## C#:## Show Code >>//This function returns True if number is prime static private bool isPrime(int number) { for (int i = 2; i < number; i++) if (number % i == 0) return false; return true; } //Ask for and save the range here Console.WriteLine("Please enter the starting point."); String startingPointAsAString = Console.ReadLine(); Console.WriteLine("Please enter the stopping point."); String stoppingPointAsAString = Console.ReadLine(); //C# reads in strings which must be converted to integers int startingPoint = Convert.ToInt32(startingPointAsAString); int stoppingPoint = Convert.ToInt32(stoppingPointAsAString); //Loop through every number in the given range for (int i = startingPoint; i <= stoppingPoint; i++) { //Check if 'i' is actually prime if (isPrime(i)) Console.WriteLine(i+ " is a prime."); } Console.WriteLine("Press enter to exit"); Console.ReadLine(); |

## Java:## Show Code >>//This function returns True if number is prime static private boolean isPrime(int number) { for (int i = 2; i < number; i++) if (number % i == 0) return false; return true; } Scanner sc = new Scanner(System.in); //used for reading in data //Ask for and save the range here System.out.println("Please enter the starting point."); int startingPoint = sc.nextInt(); System.out.println("Please enter the stopping point."); int stoppingPoint = sc.nextInt(); //Loop through every number in the given range for (int i = startingPoint; i <= stoppingPoint; i++) { //check if 'i' is prime. if (isPrime(i)) System.out.println(i + " is a prime."); } System.out.println("Exiting now"); |

## Python:## Show Code >>#This function returns True if number is prime def isPrime(number): for i in range (2,number-1): if (number % i == 0): return False return True #ask for and save the range here startingPoint = int(input("Please enter the starting point.")) stoppingPoint = int(input("Please enter the stopping point.")) #Loop through every number in the given range for i in range (startingPoint,stoppingPoint): if (isPrime(i)): print(i, "is a prime") |

Updated 6 February 2014 by Matthew F.