Click here to Skip to main content
15,888,073 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Create an application using CODIO that satisfies the output.
Enter n:   8
Enter 8 numbers: 11 20 4 33 55 17 5 33
The elements in the list which are primes
11 33 17 5 33

What I have tried:

Enter n:   8

Enter 8 numbers: 11 20 4 33 55 17 5 33

 

The elements in the list which are primes

11 33 17 5 33
Posted
Updated 20-Jan-21 4:15am
Comments
Patrice T 20-Jan-21 5:14am    
33 is not a prime !

You need a primality test function. See, for instance Primality Tests Tutorials & Notes | Math | HackerEarth[^].
 
Share this answer
 
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^] and think about how you would do it manually.
 
Share this answer
 
one possible way on the little information you've given us : you write 3 functions ..

(1) returns an array of n random numbers between lower & upper limits
(2) tests a number to see if it is a prime
(3) takes in an array of random numbers and returns an array of primes using (2)

.. and then 'glue' the functions together :simples:

if you want full marks you would generate test units for the functions eg isPrime(33) => false
 
Share this answer
 
v2
Effectively you have three parts to this problem.

1. Get a list of numbers
2. Iterate over the list
3. Check to see if the number is prime

Let's ignore the first step as you should easily be able to do this. This leaves you with the iterate and check to see if the number is a prime. Obviously, the most complicated part of this is the prime number check so you want to simplify this as much as possible. Fortunately, there are a number of well known prime generation techniques some of which are exceedingly efficient. What I would be tempted to do to solve this is find the largest number in the list first. Once you have the largest number, use a technique such as the Sieve of Eratosthenes[^] to generate all of the possible prime numbers up to that point. Iterate over your list again and check to see if the number is in the sieve you just generated. The beauty about this approach is that techniques such as sieves are relatively fast so you can throw in reasonably large numbers and calculate whether or not it's prime with ease.

As a bonus, you could be really sneaky and pre-generate every prime up to the maximum int size and use that list every time.
 
Share this answer
 
 
Share this answer
 
using System;
using System.Linq;
using System.Data.Entity;

public class Program
{
	public static void Main()
	{
		int n,num;
		Console.WriteLine("Enter n");
		n=Convert.ToInt32(Console.ReadLine());
		int[] prime =new int[n];
		
		Console.Write("Enter "+n.ToString()+" Numbers\n");
		for(int i=0;i<prime.Length;i++)
		{
			int x =  Convert.ToInt32(Console.ReadLine());
			prime[i]=x;
		}
		for(int i = 0;i<prime.Length;i++)
		{
		bool checkPrime = true;
		int a = prime[i];
		for( num = 2;num <= a/2; num++)
		{
		if(a%num == 0 )
		{
			checkPrime = false;
			break;
		}
		}
			if(checkPrime)
				Console.WriteLine("Prime number is:"+a.ToString());
			
		}
	}
}
 
Share this answer
 
v3
Comments
Dave Kreskowiak 20-Jan-21 10:48am    
Doing homework for someone is greatly frowned upon.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900