`using`

`System;`

`using`

`System.Collections.Generic;`

`using`

`System.Linq;`

`using`

`System.Text;`

`namespace`

`PrimeNumbersToFindInArrayAndCopy2Another`

`{`

` `

`class`

`Program`

` `

`{`

` `

`static`

`void`

`Main(`

`string`

`[] args)`

` `

`{`

` `

`//some random numbers`

` `

`int`

`[] a = { 34, 56, 78, 89, 43, 23, 7, 5, 99, 91, 97 };`

` `

`//target array to hold only prime numbers`

` `

`int`

`[] b = { }; `

`//as we don't know the number of elements, we will not initialize`

` `

`//traverses through each element in array 'a'`

` `

`foreach`

`(`

`int`

`n `

`in`

`a)`

` `

`{`

` `

`if`

`(isPrime(n))`

` `

`{`

` `

`//if a number is prime, place it into array 'b'`

` `

` `

`//the size of array 'b' needs to resized (increase by 1)`

` `

`Array.Resize<`

`int`

`>(`

`ref`

`b, b.Length + 1);`

` `

`//find the last location in b`

` `

`int`

`p = b.Length - 1;`

` `

`//place the number in last location of b`

` `

`b[p] = n;`

` `

`}`

` `

`}`

` `

`//display all elements in b`

` `

`foreach`

`(`

`int`

`i `

`in`

`b)`

` `

`{`

` `

`Console.WriteLine(i);`

` `

`}`

` `

`//wait for enter key`

` `

`Console.ReadLine();`

` `

`}`

` `

`static`

`bool`

`isPrime(`

`int`

`n)`

` `

`{`

` `

`bool`

`b = `

`true`

`;`

` `

`//1 is always a factor for every number (we start with 2)`

` `

`//there can never be a factor of a number which is above the half of the number.`

` `

`//ex:for a number 200, the highest factor is 100 (which is just half of 200).`

` `

`for`

`(`

`int`

`i = 2; i <= (n / 2); i++)`

` `

`{`

` `

`//if a factor is found for 'n', it is no longer a prime`

` `

`if`

`((n % i) == 0)`

` `

`{`

` `

`b = `

`false`

`;`

` `

`break`

`;`

` `

`}`

` `

`}`

` `

`return`

`b;`

` `

`}`

` `

`}`

`}`