Question

In: Computer Science

C# programming. Comment/Explain the below code line by line. I am having a hard time following...

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();

        }

    }

}

Solutions

Expert Solution

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


Related Solutions

can someone explain to me what osmolality is.? i am having a hard time understanding it
can someone explain to me what osmolality is.? i am having a hard time understanding it
I am having a hard time understanding these two questions. If someone can explain that would...
I am having a hard time understanding these two questions. If someone can explain that would be great. 1) Explain the steps that enables body to metabolize fat to ATP? 2) Write out the amount of ATP, NADH, FADH2 produced in each step of cellular metabolism of a glucose.
I understand the answer to this I am just having a hard time creating a graph...
I understand the answer to this I am just having a hard time creating a graph for it. Bill the butcher is upset because the government plans to tax beef $.10 a pound. "I hate paying taxes," he says. "Because of this, I'm raising all my beef prices by $.10 a pound. The consumers will bear this burden, not me." Do you see anything wrong with this way of thinking? Explain. Draw a graph describing your answer and attach it...
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
I am having a hard time getting started on how to do this assignment. I would...
I am having a hard time getting started on how to do this assignment. I would like some ideas on how to start the MEMO. and maybe some ideas on what you would include. But I don't necessarily need the assignment completed for me. just need ideas!!! One routine negative message in the form of an internal memo. 400-500 word message. Single-spaced, with 1-inch margins on all sides. Objective: To announce organizational restructuring, one routine negative message addressed to employees....
what is the calculated the average code using C++ Having a hard time trying to get...
what is the calculated the average code using C++ Having a hard time trying to get the float average.
I am having a hard time getting my an output after putting in the second set...
I am having a hard time getting my an output after putting in the second set of functions and I was hoping to be able to have the results from the functions be rounded to 2 decimal places. <html> <head> <title>Length Conversion</title> <script language='JavaScript' type='text/JavaScript'> <!-- function validate(type) { if(document.my_form.textinput.value=='') { alert('Fill the Input box before submitting'); return false; }else{ if(type=="to_feet"){ var res=3.2808*document.my_form.textinput.value; var unit=" feet"; }else{ var res=0.3048*document.my_form.textinput.value; var unit=" meter"; } document.getElementById("result").innerHTML=res.toFixed(2) + unit; return false; } }...
For some reason I am having a hard time getting this program to compile properly. Could...
For some reason I am having a hard time getting this program to compile properly. Could you help me debug it? Write the prototypes and functions to overload the given operators in the code //main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() {     rectangleType rectangle1(23, 45);                     //Line 1     rectangleType rectangle2(12, 10);                     //Line 2     rectangleType rectangle3;                             //Line 3     rectangleType rectangle4;                             //Line 4     cout << "Line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line is replacing all the '1' values across my .csv file and is throwing off my data aggregation. How would I implement 'iloc' so that this line of code only changes the '1's integers in my 'RIC' column? patient_data_df= patient_data_df.replace(to_replace=[1], value ="Stroke") Thank you:)
TCP client and server using C programming I am having trouble on how to read in...
TCP client and server using C programming I am having trouble on how to read in the IP adress and port number from the terminal Example: Enter IP address: 127.0.0.1 Enter Port Number: 8000 in both client and server code. How do can I make I can assign the Ip address and port number using the example above. the error I get is that the client couldn't connect with the server whenever i get the port number from the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT