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....
I have figured out this assignment, but I am having a hard time figuring out the...
I have figured out this assignment, but I am having a hard time figuring out the extra credit, Thanks! Creating a Rectangle class We have created a representation of a rectangle in C++ and extended it to create multiple representation's of rectangles. Classes can simplify that process considerably. In this assignment, your task is to convert the representation of a rectangle we created earlier in the semester to a Rectangle class. The Rectangle class shall consist of the following: The...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
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; } }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT