Program runs fine but i want it to be able to read an array and store the first 100 prime numbers. Anyone have any idea how?

/*
 * Name: Sean
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variables
            int value;

            //Initialize
            Console.WriteLine("Enter Integar: ");
            value = Convert.ToInt32(Console.ReadLine());
            //Compute
            checkForPrime(value);
            
            //Output
           
            //Freeze
            Console.ReadKey();
        }

        public static int checkForPrime(int value)
        {
            int count;
            int count2;
            int prime;
            for (count = 2; count < value; count++) 
            {
                prime = 1; 
                for (count2 = 2; count2 < count; count2++) 
                {
                    if (count % count2 == 0) 
                    {
                        prime = 0; 
                    }
                }
                if (prime == 1) 
                    Console.WriteLine("{0} Is Prime Number", count);
            }
            return value;
        }
    } 
}

GoogleEyedBass,

A loop of some sort should suffice, such as for or while.

Something like

int i = 0;

int[] numbers = int[100];//array to hold first 100 items


  for(int j = 0; j < ArrayOfNumbersToTest.Count; j ++)
  {
    if(//check if number in initail array is prime here)
    {
      //add the number from initial to array of 100;  
      i ++;//increment counter for number of ints in the numbers array
    }

    if(i > 100)
      //logic to break loop here
  }

I left out a bit for you to do yourself, but it should give you the general idea.

You may need to change the way that your prime number calculator works in order to make the idea work. A different return type should do the trick.

Hope that helps.

Edited 6 Years Ago by croker10: n/a

Comments
Thanks
This article has been dead for over six months. Start a new discussion instead.