Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
C#
<pre lang="c#">
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

Yes it right. and it is working properly in C# command prompt.
 
Share this answer
 
Hello

C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900