-4

I have an assignment to write a code that prints out all prime numbers between 1-100. I am looking at different programs to get an idea on what to do and keep running into i.e. "if (x%c == 0)".

Now I can't find out what the "x%c" means. I've looked around a lot and can't find any good answers anywhere. Possibly because of searching for the wrong thing. What exactly does that do in the following code?

#include<stdio.h>

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;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
SuddenlyJ
  • 13
  • 2
  • 6
    Google *c operators* – Yu Hao Dec 23 '16 at 09:06
  • 5
    You really can't learn a programming language effectively without at least one good reference book - see [this list](http://stackoverflow.com/q/562303/253056). – Paul R Dec 23 '16 at 09:11
  • 2
    Sorry, this question show zero research effort. example [google search for percentage sign as c operator(https://www.google.co.in/search?q=percentage+sign+as+c+operator&oq=percentage+sign+as+c+operator&aqs=chrome..69i57.7449j0j1&sourceid=chrome&ie=UTF-8) yields this first two results as SO posts. – Sourav Ghosh Dec 23 '16 at 09:15
  • @PaulR Please don't recommend that book list. Arbitrary people keep adding arbitrary books to it, out of the blue. It doesn't seem to work having a book recommendation list as community wiki, people keep abusing it. – Lundin Dec 23 '16 at 09:23
  • @Lundin: oh - that's a shame - any better ideas for recommending a specific book or list of books - it seems to be something that's often needed ? – Paul R Dec 23 '16 at 09:24
  • @Lundin: how about just the accepted answer to that same question about C books - is that a better reference ? – Paul R Dec 23 '16 at 09:31
  • @PaulR Ideally we'd have some kind of feature for community recommendations / community blacklist which was based on votes. Originally that thread was organized in that way, so that you'd suggest a book and then people got to vote. But moderators have destroyed the whole thread now, it is beyond repair. – Lundin Dec 23 '16 at 09:31
  • 1
    @Lundin: I wonder if such a reference would be a good fit for the new [Documentation](http://stackoverflow.com/documentation/c/topics) area ? I haven't participated in this yet, but it seems like it would be a good place to list useful/reliable reference material ? – Paul R Dec 23 '16 at 09:48
  • @PaulR Documentation suffers from severe quality problems so far. There's several active discussions on meta about it right now. – Lundin Dec 23 '16 at 09:54

2 Answers2

2

It is the modulo operation. https://en.wikipedia.org/wiki/Modulo_operation

It is the remainder, if you do a division in integer space.

for example:

3 % 3 = 3 mod 3 = 0

means if you divide 3 by 3 you get one and the remainder is 0. If the division leaves no remainder the first number is a multiple of the second. That means it is not a prime (if the number you divide with is not 1 or the same number)

A full example

15 % 2 = 1
15 % 3 = 0 

15 is no prime

Kami Kaze
  • 2,069
  • 15
  • 27
1

"percentage in-between variables" is the operator for finding the remainder in C.

For example, if x = 10 and y = 4, then, x % y would be 2.

for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }

In the code snippet above, we will break out of this loop when a value of c is reached such that i is divisible by c i.e. i when divided by c gives 0 as the remainder.

skrtbhtngr
  • 2,223
  • 23
  • 29