1

I've just started learning C# and I am trying to make a program that would find the sum of all the prime numbers between two numbers that a person enters. But my program is always stuck on 3 or 5, whatever I try to do. I would really appreciate an explanation about what I am doing wrong and how to fix it because I am stuck for two days already and don't have any clue. Thank you!

using System;

public class MainClass
{
    public static void Main()
    {
        int startValue = int.Parse(Console.ReadLine());
        int endValue = int.Parse(Console.ReadLine());
        int number = startValue;
        int primeNumber = 1;
        int sum = 0;

        while (number < endValue)
        {
            if (((number % 2 == 0) && (number != 2)) || ((number % 3 == 0) && (number != 3)) && (number > 1)) //looking for a prime number
            {
            }
            else
            {
                primeNumber = number;
                sum = sum + primeNumber;
            }
            number = primeNumber + 1;
            Console.WriteLine($"{sum} and {primeNumber}");
        }
        Console.WriteLine($"Sum of the prime numbers= {sum}");
    }
}

As I am not very experienced, I was trying to change some code here and there, change its place, and change 'while' with 'for'. No luck so far

darclander
  • 1,526
  • 1
  • 13
  • 35

2 Answers2

1

It seems like @NishanDhungana already provided a better version of your code but to answer the:

"But my program is always stuck on 3 or 5, whatever I try to do. I would really appreciate an explanation about what I am doing wrong"

I would firstly suggest that you check out how to debug (provided by @DourHighArch) but to make this simple you can use print statements to easily understand what is happening.

If we take a look at your code we see that the loop while (number < endValue) will continue as long as number is less than endValue. So in order for the loop to stop, number should be >= endValue. Let's assume that startValue = 0, endValue = 10. With this assumption number will be 0 in the first iteration. In your code it will not be detected as a prime number so we continue. You update number = primeNumber + 1 which means that number = 1 + 1. As you said this works for 2 and 3, so let's take a look at what happens when number = 3.

For number = 3 we enter the else and primeNumber = 3, sum is increased and we set number = primeNumber + 1. Now this is where your loop fails. number = 4, and 4 < 10 so we are still in the while loop. Four is divisible by two and it is not two so we do not enter the else. Instead we set number = primeNumber + 1 again. But primeNumber has not changed since the last time you set it to 3. So number is again set to 4 and therefore you are stuck in an infinite loop.

I would suggest that for simplicity (if my wall of text was too much rambling), that you print every variable in each iteration of the while loop and try to understand what is happening. Why are values updated or why do they remain the same? The issue is that after setting number to 4 it is no longer updated.

darclander
  • 1,526
  • 1
  • 13
  • 35
  • @VladMikhalkin I am glad it makes sense! :D Did this answer your question completely? (I wouldn't want to *steal* the accepted answer but it could help future users with the same problem). – darclander Apr 08 '23 at 15:01
0

Try this one...

   public class Program
{
    public static void Main(string[] args)
    {
        int startValue = 1;
        int endValue = 100;
        int sum = 0;

        while (startValue <= endValue)
        {
            if (Program.IsPrime(startValue)) {
                sum += startValue;
            }

            startValue++;
        }

        Console.WriteLine("Sum of prime number is: " + sum);
    }

    static bool IsPrime(int number)
    {
        if (number < 2) return false; // Prime numbers start at 2
        if (number == 2 || number == 3) return true; // 2 and 3 are prime
        if (number % 2 == 0 || number % 3 == 0) return false; // multiples of 2 and 3 are not prime

        // Check odd numbers up to the square root of the input number
        for (int i = 5; i * i <= number; i += 6)
        {
            if (number % i == 0 || number % (i + 2) == 0)
            {
                return false;
            }
        }
        return true;
    }
}
Nishan Dhungana
  • 831
  • 3
  • 11
  • 30
  • Since the _op_ is a newbie, can you make a version that does not use `List` and just has a running sum as the original post is trying to do? – John Alexiou Apr 07 '23 at 04:11
  • The problem is I am ok with my prime number searching. It seems to be working well as I tried it multiple times. But the loop isn't working and I rll can't understand why – Vlad Mikhalkin Apr 07 '23 at 05:10