-3

I have written this code for C to print prime numbers from 1-300

GNU GCC Compiler shows the following error:

error: invalid operands to binary % (have ‘double’ and ‘int’)

if (sqrt(num) == 0 || sqrt(num) % 2 != 0)

My code is this:

#include <stdio.h>
#include <math.h>

int main() {
  /*Created by Suvid Singhal                       Date:- January 2, 2017*/
  int num;
  printf("Welcome to 1-300 prime numbers C Program!!!");
  for (num = 0; num <= 300; num++) {
    if (sqrt(num) == 0 || sqrt(num) % 2 != 0) {
      printf("%d\n", num);
    } else {
      continue;
    }
  }
  return 0;
}
Stargateur
  • 24,473
  • 8
  • 65
  • 91

4 Answers4

4

You have lots of thing going wrong in your answer.

Let's check them one by one.

  • Your idea about prime number is not clear. A prime number has no divisior except the number and 1. (1 is not a prime number).

How to check if a number is prime or not?

You can check all the `numbers <=square root of (x). So you need to check if someone (except 1 ) divides the number. If yes then it is not prime else it is.

Implementation Details

int check= 0;
for(int i=2;i<=(int) sqrt(x);i+=1)
  if(n%i == 0)
  { 
     check = 1;
     n is non-prime;
     break; // no need to check 
  }
if(check == 0)
  n is prime

sqrt() return double and ...

Modulous can be applied over an int so you have to cast it to that.

If you want to avoid it all

for(int i=2;i*i<=x;i++)
...

For you to study

Learn about sieve method to get the primes. For 300 numbers probably it doesn't matter what you use but in case it is 10000000 then you will definitely want to give it a read.

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

% operator is for integers. You may want to use fmod() or an integer cast.

if ( sqrt( num ) == 0 || fmod( sqrt( num ), 2 ) != 0 )

or

if ( sqrt( num ) == 0 || (int) sqrt( num ) % 2 != 0 )

This should fix your compilation errors.

ps: If you are looking for an awesome way to generate prime numbers, see Sieve of Erastosthenes

Márcio
  • 26
  • 2
0

In the code sample you have given, it is not quite clear how you intend to determine whether a number is prime or not. You have the following condition if (sqrt(num) == 0 || sqrt(num) % 2 != 0) to apparently determine a number as prime. But this condition is syntactically invalid. As others have pointed out, you can't do modulo operation on a non-int number as returned by the sqrt function. What value, do you think, is returned by the operation sqrt(num) % 2 and what has it do with a number being prime? Your usage of continue has also no importance. Maybe you are a little confused between break and continue. I am giving you a sample program. Try to learn what your mistakes are from this:

int isPrime = 1;
for(int i=2;i<=300;i++) {
    isPrime = 1;
    for(int j=2; j!=i && j <= ceil(sqrt(i));j++) {
        if(i%j == 0) {
            //Not a prime number
            isPrime = 0;
            break;
        }
    }
    if(isPrime) {
        printf("%d\n", i);
    }
}
VHS
  • 9,534
  • 3
  • 19
  • 43
-1

Changing

if( sqrt(num) == 0 || sqrt(num) % 2!=0 )

to

if( sqrt(num) == 0 || (int)sqrt(num) % 2!=0 )

Should fix compilation errors. See error: invalid operands to binary % when taking modulus of float for more info.

Community
  • 1
  • 1
Sush
  • 1,169
  • 1
  • 10
  • 26