Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to find prime and non-prime numbers in an array with C#

1 Answer

0 votes
using System;

internal class Program
{
	public static bool isPrime(int num) {
		for (int i = 2; i <= num / 2; i++) {
			if (num % i == 0) {
				return false;
			}
		}
		return true;
	}

	public static void Main(string[] args)
	{
		int[] arr = new int[] {23, 87, 100, 47, 71, 897, 228, 3001, 4325, 8797, 5361};
		int size = arr.Length;

		for (int i = 0; i < size; i++) {
			if (isPrime(arr[i])) {
				Console.Write("{0,3:D} - Prime\n", arr[i]);
			}
			else {
				Console.Write("{0,3:D} - Not Prime\n", arr[i]);
			}
		}
	}
}



/*
run:
 
 23 - Prime
 87 - Not Prime
100 - Not Prime
 47 - Prime
 71 - Prime
897 - Not Prime
228 - Not Prime
3001 - Prime
4325 - Not Prime
8797 - Not Prime
5361 - Not Prime
 
*/

 



answered Feb 19, 2024 by avibootz
...