0

How would the code for this look like in C ? I did try doing it on javascript but did not know how too loop it. A program that determines if a number that the user has entered is a prime number. The program will continue to ask for numbers until the user enters a value less than 2. This program must be implemented using modules. example :

Enter a number: 4
4 is not a prime number 
Enter a number: 5
5 is a prime number 
Enter a number: 0

int main()
{
   int n, i = 3, count, c;

   printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);

   if ( n >= 1 )
   {
      printf("First %d prime numbers are :\n",n);
      printf("2\n");
   }

   for ( count = 2 ; count <= n ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
         printf("%d\n",i);
         count++;
      }
      i++;
   }

   return 0;
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
user2880024
  • 11
  • 2
  • 7
  • @RahulTripathi That part probably means the user enters `5`, and the program outputs `5 is not a prime number`. EDIT: looking at the post source shows that's the case. – millimoose Oct 14 '13 at 19:16
  • @RahulTripathi That's `4` (<<= user entered this) followed by `4 is not a prime number` (<<== computer printed this) – Sergey Kalinichenko Oct 14 '13 at 19:17
  • Formatted the question!! – Rahul Tripathi Oct 14 '13 at 19:19
  • 2
    [How many more projects in your class?](http://stackoverflow.com/questions/19367049/a-program-that-prompts-a-user-to-enter-the-hour-minute-and-either-am-or-pm) Knowing that, I can reallocate my spare time. – Jongware Oct 14 '13 at 19:50

2 Answers2

1

This is general not optimized algorithm:

int n;
do
{
    printf("Enter the number: ");
    scanf("%d",&n);

    if (n >= 2) // check if number is prime, general algorithm
    {
        bool prime = true;
        for (int j=2; j<n; j++)
            if ( (n%j) == 0) // n is divided by j without modulus - is it not prime
            {
                prime = false;
                break;
            }

        if (prime)
            printf("prime\n");
        else
            printf("is not prime\n");
    }
} while (n >= 2);

and here is updated version, which uses fact that all even numbers are not prime, and there is no sense to check numbers which are greater N/2

int n;
do
{
    printf("Enter the number: ");
    scanf("%d",&n);

    if (n >= 2) // check if number is prime, general algorithm
    {
        bool prime = true; // for n=2 prime is true
        if (n > 2)
            prime = n & 1; // number at least odd

        if (prime)
        {
            // starts with 3
            for (int j=3; j<=(n>>1); j+=2)
                if ( (n%j) == 0) // n is divided by j without modulus - is it not prime
                {
                    prime = false;
                    break;
                }
        }

        if (prime)
            printf("prime\n");
        else
            printf("is not prime\n");
    }
} while (n >= 2);
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

actually it is enough to check till sqrt(n) which will make your code run much faster.

you can look at this post for other useful methods.

Community
  • 1
  • 1
RonenKr
  • 213
  • 1
  • 7