By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
Write a program to take a positive number n as an input and find the nth prime number. For example, if the user enters 20, the output should be 71 (20th prime number).
Marking Scheme
Writing down how your program will solve the task
Pseudo code
C# code
Indentation in the code
Comments in the code
Presentation
using System;
public class Test
{
publicstatic void Main()
{
Console.WriteLine(getPrimeForNumber(12));
}
publicstatic bool isPrime(int number){
for(int i=2;i<=number/2;i++){
if(number%i==0){
returnfalse;
}
}
returntrue;
}
publicstatic int getPrimeForNumber(int number){
inti=1;
intcounter = 0;
while(counter<number){
i++;
if(isPrime(i)){
counter++;
}
}
returni;
}
}
Pseudocode:
Input number
Go through all numbers
If prime then increment counter
If counter equal to user input, thenoutput result
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
C#