Results 1 to 3 of 3

Thread: Display Prime Number in C#

  1. #1
    TorresScott is offline Senior Member
    Join Date
    Dec 2009
    Posts
    239
    Rep Power
    3

    Default Display Prime Number in C#

    Hi. I am first year MCA Student. I am learning C# programming language. Right now I have little bit knowledge of C#. I want to write a program for to find a prime number. I tried different way but none of them worked out. Any help would be highly appreciated. Thanks in advanced.

  2. #2
    AllenBrown is offline Senior Member
    Join Date
    Dec 2009
    Posts
    240
    Rep Power
    3

    Default

    Refer following program to display Prime Number program in C# :
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication16
    {
        class Program
        {
    
    
            void prime_num(long num)
            {
    
                bool isPrime = true;
                for (int i = 0; i <= num; i++)
                {
                    for (int j = 2; j <= num; j++)
                    {
                        if (i != j && i % j == 0)
                        {
                            isPrime = false;
                            break;
                        }
                    }
                    if (isPrime)
                    {
                    Console .WriteLine ( "Prime:" + i );
                    }
                    isPrime = true;
                }
            }
    
    
    
            static void Main(string[] args)
            {
                Program p = new Program();
                p.prime_num (999999999999999L);
                Console.ReadLine();
    
            }
        }
    }

  3. #3
    HallMiller is offline Senior Member
    Join Date
    Dec 2009
    Posts
    251
    Rep Power
    3

    Default

    Try out this code, when you run this program you get the prime number as an output result:
    Code:
    int counter;
    int counter2;
    float ansFloat;
    int ansInt;
    float check;
    check = 1;
    for (counter = numStart; counter <= numEnd; counter++)
    {
    for (counter2 = 2; counter2 < counter; counter2++)
    {
    ansFloat = counter / counter2;
    ansInt = counter / counter2;
    check = ansInt / ansFloat;
    while (check == 1)
    {
    // Don't do anything.
    }
    }

Similar Threads

  1. C code for given number is Armstrong number or not
    By RussellBarnes in forum Programming
    Replies: 1
    Last Post: 05-29-2010, 12:01 PM
  2. Replies: 2
    Last Post: 05-14-2010, 01:30 PM
  3. Program to write prime factor of given number:
    By BrooksGray in forum Programming
    Replies: 2
    Last Post: 01-14-2010, 06:18 PM
  4. Replies: 0
    Last Post: 09-21-2009, 05:35 PM
  5. Token Number Display for Banks
    By harddisk6564 in forum Sound Card
    Replies: 0
    Last Post: 02-17-2009, 06:34 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48