Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to make this exercises with c#, can you help me?, pleaseee

//a. Given a list of numbers:
//a. Find the largest even number in the listing.
//b. Find the smallest prime in the listing in case there is only one show that one and if there is none show the appropriate message.


I only have the fist step

I will rate your answer

What I have tried:

int a = 0, b;

string[] listado = Console.ReadLine().Split(",");

for (int i = 0; i < listado.Length; i++)
{
    b = int.Parse(listado[i]);
    if (b % 2 == 0)
    {
        if (b > a)
        {
            a = b;
        }
        else
        { }
Posted
Updated 24-Feb-22 11:55am
v2
Comments
Patrice T 22-Feb-22 1:10am    
Incomplete code, again.
darelin rijo hernandez 22-Feb-22 1:12am    
This is what I need, the second part, because I don't know how to continue with the second part
Richard MacCutchan 25-Feb-22 3:51am    
For all odd numbers you need to find out if they are prime or not.

Let's make things easy for you.

Your approach toward the solution should be like

1. Make a function "FindLargestEvenNumber", this function will accept one List<int> parameter and will return an integer
2. In the above function you iterate over list<int).
2.1 create int outVaue = 0 
2.2 For each iteration you check whether the number is even. If it is then store it in a variable _t. check if _t > outValue. If it is then outValue = _t;
2.3 the outValue should contain the largest even number. return it from the function
3. Create function "FindSmalledPrimeNumber" function, this function will accept one List<int> parameter and will return an integer at the end of iteration
3.1 create int outValue = 0
3.2 For each iteration over List<int>, check if the number is prime. You need to check definition of prime
3.3 If the number is prime then store it in _t variable. If _t > outValue then outValue = _t
3.4 return outVaue at the end of iteration
4. Function Main()
4.1 Read all numbers from the console and cast them in List<int> intArray
4.2 Print FindLargestEvenNumber(intArray)
4.3 Print FindSmallPrimeNumber(intArray)
5 End Program


That's the first version of the solution. Once you do that then comes the optimization phase. For example, this entire solution time complexity is O(2n). The first optimization iteration would be to do it in O(n) and then things go interesting from here.

Hoping this would give you a quick start.

Happy Coding
 
Share this answer
 
While we are more than willing to help those that are stuck, 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.
Start with Wiki and prime numbers: Prime number - Wikipedia[^] then think about how you would identify if a given number is prime or not.

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[^]
 
Share this answer
 
public static int FindLargestEven(List<int> collection)
{
    List<int> nums = new List<int>();
    int result = -1;
    for(int i = 0; i < collection.Count; i++)
    {
        int num = collection[i];
        if(num %2 == 0)
        {
            nums.Add(num);
        }
    }            
    for(int i = 0; i < nums.Count; i++)
    {
        result = i == 0 ? nums[i] : nums[i] > result ? nums[i] : result;
    }
    return result;
}
 
Share this answer
 

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