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

Hello, I am just having a hard time understanding this question. The question is below. I...
Hello, I am just having a hard time understanding this question. The question is below. I am not supposed to write a program but instead in regular english sentence form just name the methods, fields, and variables I would use for this loan class. Im not really sure what a loan class means as well, thank you for your help. USING JAVA Given a Loan class, name the necessary fields and methods that we would need in this class. Be...
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.
C++ I am a beginner at this and have a hard time with for loops.. I...
C++ I am a beginner at this and have a hard time with for loops.. I was given this prompt. part 1. 1. Create an array to store 8 characters. 2. Using a for loop, get an 8-letter word from the user, one letter at a time. 3. Then use another for loop to print the complete word onto the screen one letter at a time. 4. Then use a third for loop to print out the word backwards. part...
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 am having a hard time writing these SQL queries. Please specify the following queries in...
I am having a hard time writing these SQL queries. Please specify the following queries in SQL on the database schema shown in the figure below. STUDENT Name StudentNumber Class Major Smith 17 1 CS Brown 8 2 CS Kathy 15 1 EE COURSE CourseName CourseNumber CreditHours Department Intro to Computer Science CSE110 4 CS Data Structures CSE205 4 CS Discrete Mathematics MAT240 3 MATH Databases CSE380 3 CS Analog Circuits EE260 3 EE SECTION SectionIdentifier CourseNumber Semester Year Instructor...
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...
Below is the assignment that I need to do and having a hard time finding the...
Below is the assignment that I need to do and having a hard time finding the correct ratios like Gross Margin, EBITD, Price to cash flow etc.. from SEC EDGAR. I am comparing CVS Health and Walgreens 2018. Below is the question similar to mine that I found on the website. I am trying to figure out where can I find those ratios? MBA 520 Module Two Activity Guidelines and Rubric Overview: For this task, you will analyze the financial...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT