Results 1 to 3 of 3

Thread: Prime Number programme in C#

  1. #1
    Join Date
    Nov 2008
    Posts
    19

    Prime Number programme in C#

    Hello friends

    Can some post here Prime Number programme in C#. i tried that but it giving me error.

    Thanks

  2. #2
    Join Date
    Jul 2008
    Posts
    20

    Re: Prime Number programme in C#

    Here is C# code to display given no is Prime Number or not


    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Exercise_1
    {
        class Prime
        {
            public void Prime_Number(int number)
            {
                int i;
                for (i = 2; i < number; i++)
                {
                    if (number % i == 0)
                    {
                        Console.WriteLine("{0} is not a prime number", number); 
                        break;
                    }
                }
                if(i == number)
                {
                    Console.WriteLine("{0} is a prime number", number);
                }
            }
            static void Main(string[] args)
            {
                int num;
                Console.WriteLine("Enter the number: ");
                num = Convert.ToInt32(Console.ReadLine());
                Prime p = new Prime();
                p.Prime_Number(num);
            }
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    22

    Re: Prime Number programme in C#

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool current = false;
                int j;
    
                Console.WriteLine("Enter any integer");
                int num = Int32.Parse(Console.ReadLine());
    
                for (int i = 2; i <= num; i++)//2 is the first <strong class="highlight">prime</strong> <strong class="highlight">number</strong>.
                                              //I set i to 2 beacuse it has to print 2 firstly...  
                {
                    for (j = 2; j < i; j++)
                    {
                        if (i % j == 0)//Controls i is <strong class="highlight">prime</strong> <strong class="highlight">number</strong> or not...
                        {
                            current = true;
                            break;//breaks for not controlling anymore...
                        }
                    }
                    if (current == false)
                        Console.Write("{0} ", j);//if i is <strong class="highlight">prime</strong> <strong class="highlight">number</strong>, print it...
                    else
                       current = false;   
                }
    
                Console.ReadLine();
    
    
    
            }
        }
    }

Similar Threads

  1. Problem with C# program which calculates the prime number
    By $Bird$ in forum Software Development
    Replies: 4
    Last Post: 29-11-2010, 11:54 AM
  2. Shell program for the prime number
    By Rup_me in forum Software Development
    Replies: 5
    Last Post: 14-12-2009, 09:24 AM
  3. program to print prime factor of given number
    By teena_pansare in forum Software Development
    Replies: 4
    Last Post: 27-11-2009, 09:21 AM
  4. Check whether an integer is a prime number in C
    By Projectkmo in forum Software Development
    Replies: 2
    Last Post: 08-10-2008, 06:27 PM
  5. Prime number
    By mad4jack in forum Software Development
    Replies: 3
    Last Post: 03-10-2008, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •