mardi 10 novembre 2020

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?

Aucun commentaire:

Enregistrer un commentaire