简体   繁体   中英

How to find prime numbers between two intervals? I made one but this doesn't work

C programming beginner here. I have an assignment in which I have to find the prime numbers between two intervals. Here is my code:

#include <stdio.h>
int main() {
  // This is a program to calculate the prime number between two numbers
  printf("Enter two numbers!:");
  int a, b, i, j, num1, num2;
  scanf("%d%d", &a, &b);
  if (a < b) {
    for (num1 = (a + 1); num1 < b; num1++) {
      for (i = 2; i < num1; i++) {
        if ((num1 % i) == 0)
          break;
        else
          continue;
      }
      if ((num1 % i) == 0)
        continue;
      else {
        printf("%d between %d and %d is a prime number!\n", num1, a, b);
        continue;
      }
    }
  } else if (b < a) {
    for (num2 = (b + 1); num2 < a; num2++) {
      for (j = 2; j < num2; j++) {
        if ((num2 % j) == 0)
          break;
        else
          continue;
      }
      if ((num2 % j) == 0)
        continue;
      else {
        printf("%d between %d and %d is a prime number!\n", num2, b, a);
        continue;
      }
    }
  }
  return 0;
}

This yields output:

Enter two numbers!:20
30

What is wrong in this? I have been doing this for many days. I also tried using the goto statement but without any progress. Maybe I have been doing this all wrong. What can be done in this?

Every number is divisible by itself (without a remainder). So, when your inner for loops (such as for (i = 2; i < num1; i++) {... ) reach their conclusion without a break-out, then the loop index ( i in this case) will be equal to num1 ... because that's when you told the loop to stop. Thus, in the following if ((num1 % i) == 0) , num1 and i will be the same, giving a zero remainder and skipping the display of the found prime number.

To fix this, just test that i and num1 aren't the same, first (and similarly for j and num2 in the second part of the code).

Furthermore, your two else... continue 'blocks' in those two inner loops are entirely redundant, as there is nothing else in the loops' bodies to 'skip'.

Try this:

int main()
{
    //This is a program to calculate the prime number between two numbers
    printf("Enter two numbers!:");
    int a, b, i, j, num1, num2;
    scanf("%d%d", &a, &b);
    if (a < b) {
        for (num1 = (a + 1); num1 < b; num1++) {
            for (i = 2; i < num1; i++) {
                if ((num1 % i) == 0)
                    break;
            }
            if (i != num1 && (num1 % i) == 0) // If i == num1, then we've found a prime number!
                continue;
            else {
                printf("%d between %d and %d is a prime number!\n", num1, a, b);
                continue;
            }
        }
    }
    else if (b < a) {
        for (num2 = (b + 1); num2 < a; num2++) {
            for (j = 2; j < num2; j++) {
                if ((num2 % j) == 0)
                    break;
            }
            if (j != num2 && (num2 % j) == 0)
                continue;
            else {
                printf("%d between %d and %d is a prime number!\n", num2, b, a);
                continue;
            }
        }
    }
    return 0;
}

Feel free to ask for any further clarification and/or explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM