In: Computer Science
C# programming. Comment/Explain the below code line by line. I am having a hard time following it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nth_prime
{
class Program
{
public static bool isPrime(int number)
{
int counter = 0;
for (int j = 2; j < number; j++)
{
if (number % j == 0)
{
counter = 1;
break;
}
}
if (counter == 0)
{
return true;
}
else
{
return false;
}
}
static void Main(string[] args)
{
int num=1;
int count = 0;
Console.Write("Number : " );
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
while (true)
{
num++;
if(isPrime(num))
{
count++;
}
if(count==n)
{
Console.WriteLine(n+"th prime number is "+num);
break;
}
}
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nth_prime
{
// This program will read number from the user and prints the nth
prime number
class Program
{
// this method will takes a number and checks if it is prime
public static bool isPrime(int number)
{
int counter = 0;
//iterating and finding the factors the for given number
for (int j = 2; j < number; j++){
if (number % j == 0){
counter = 1;
break;
}
}
//if it has 0 factors than it is prime
if (counter == 0){
return true;
}
else{
return false;
}
}
static void Main(string[] args){
int num=1;
int count = 0;
//reading number from the user
Console.Write("Number : " );
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
// looping until we found the nth prime
while (true){
num++;
//checking if n is prime than n increase count
if(isPrime(num)){
count++;
}
//checking if it is nth count than printing that number
if(count==n){
Console.WriteLine(n+"th prime number is "+num);
break;
}
}
Console.ReadKey();
}
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me