Click here to Skip to main content
14,335,077 members
Rate this:
Please Sign up or sign in to vote.
See more: (untagged)
<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace p2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the number in which you want ot find the prime no");
            int a = Convert.ToInt32(Console.ReadLine());
            bool isPrime = true;

            for (int i = 1; i <= a; i++)
            {
                for (int j = 2; j <= a; j++)
                {

                    if (i != j && i % j == 0)
                    {
                        isPrime = false;

                        break;
                    }


                }

                if (isPrime)
                {
                    Console.WriteLine("Prime:" + i);
                }

                isPrime = true;
            }

            Console.ReadLine();
        }
    }
}

Please check the code is right or not?
Posted
Updated 29-Sep-16 8:41am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Yes it right. and it is working properly in C# command prompt.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hello

using System;

using System.Collections.Generic;

using System.Text;



namespace SoftwareAndFinance

{

    class Math

    {



        static bool IsPrimeNumber(int num)

        {

            bool bPrime = true;

            int factor = num / 2;



            int i = 0;



            for (i = 2; i <= factor; i++)

            {

                if ((num % i) == 0)

                    bPrime = false;

            }

            return bPrime;

        }



        static void Main(string[] args)

        {



            Console.WriteLine("List of prime numbers between 0 - 1000");

            for (int i = 0; i < 1000; i++)

            {

                if (IsPrimeNumber(i) == true)

                    Console.Write("{0,8:n}\t", i);

            }

            Console.WriteLine();

        }

    }

}


Happy Coding
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100