Answer to Question #272749 in C# for Hilok

Question #272749

2. Create a program that identifies the first 10 prime numbers.

1
Expert's answer
2021-11-28T18:41:33-0500
using System;
using System.Collections.Generic;


namespace App
{
    class Program
    {




        static bool isPrime(int num)
        {
            if (num < 2)
            {
                return false;
            }
            if (num == 2 || num == 3)
            {
                return true;
            }
            for (int i = 2; i <= Math.Sqrt(num * 1.0); i++)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }




        static void Main(string[] args)
        {


            int number=1;
            int counter=0;
            Console.WriteLine("The first 10 prime numbers are:");
            while (counter < 10) {
                if (isPrime(number)) {
                    counter++;
                    Console.WriteLine("{0}", number);
                }
                number++;
            }
            


            Console.ReadLine();
        }
    }
}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS