Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Prime Number programme in C#

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 14-11-2008
Member
 
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

Reply With Quote
  #2  
Old 14-11-2008
Member
 
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);
        }
    }
}
Reply With Quote
  #3  
Old 14-11-2008
Member
 
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();



        }
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Prime Number programme in C#"
Thread Thread Starter Forum Replies Last Post
Problem with C# program which calculates the prime number $Bird$ Software Development 4 29-11-2010 12:54 PM
Shell program for the prime number Rup_me Software Development 5 14-12-2009 10:24 AM
program to print prime factor of given number teena_pansare Software Development 4 27-11-2009 10:21 AM
Check whether an integer is a prime number in C Projectkmo Software Development 2 08-10-2008 07:27 PM
Prime number mad4jack Software Development 3 03-10-2008 10:24 PM


All times are GMT +5.5. The time now is 02:23 AM.