1

Now im having problems with the new code in terms of compiling. I have two great answers but chux's answer is addressed to rectify my code . So by his/her directions my new code is:

#include <math.h>
#include <conio.h>
int main()
{
    int n,i,r;

    printf("Enter A Number to know its prime or non prime");
    scanf("%d",&n);
    for(i=2;i<=n-1;i++)
    {
        if(n%i==0) 
        {r==1;
        break;
    }
    }

    if(r==1)
    printf("%d is a non-prime number",n);
    else 
        printf("%d is a prime number",n);
return 0;
}

But on the output it show as 87 is a prime number. I don't know why. But can someone spot my mistake?

user187604
  • 148
  • 8
  • 2
    `=` means assignment. `if (r=1)` sets `r` to `1` and always runs. You mean `if (r)`. Also, having an `else` in the loop that sets `r = 0` doesn’t make sense, so remove that. – Ry- Feb 10 '19 at 03:03
  • @Ry- i see. Should i edit it in the question? Or keep the same its my first question so--- – user187604 Feb 10 '19 at 03:05
  • 1
    Depends on whether there’s still a problem after that. If there is, you should edit it to reflect your new code and actually describe the symptoms in the edit. See also [ask]. – Ry- Feb 10 '19 at 03:07
  • Possible duplicate of [C - determine if a number is prime](https://stackoverflow.com/questions/1538644/c-determine-if-a-number-is-prime) – Ratata Tata Feb 10 '19 at 03:25
  • @RatataTata, just because OP is trying to solve the same problem, it is not a duplicate. Sure there might be duplicates of the real issue OP is facing, but this sure isn't one. – Ajay Brahmakshatriya Feb 10 '19 at 03:35
  • @AjayBrahmakshatriya I would not like to give him a down vote and discourage him to make more questions. I'm showing him that he could learn from another answer. But it's possible duplicated. – Ratata Tata Feb 10 '19 at 03:48
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – phuclv Feb 11 '19 at 01:35

2 Answers2

5

At few problems

Assignment vs. compare

if (r=1) assigns 1 to r, so if (r=1) is always true. Certainly a compare was needed, @Ry

// if (r=1)
if (r == 1)

No early break

OP's code: The value of r depends on the last iteration. Certainly once a factor is found, loop should exit.

for(i=2;i<=n-1;i++) {
  if(n%i==0)
    // r=1;
    { r = 1; break; }
  else 
    r=0;
}

Incorrect functionality for n == 0,1

All values n < 2 incorrectly report as prime.

Inefficient

Code performs up to n loops. Only need to perform sqrt(n) loops. Tip: Do not use floating point math here for an integer problem.

// for(i=2;i<=n-1;i++)
for(i = 2; i <= n/i; i++)

Alternate

Only peek if you must code.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I don't get the sqrt n point can you please elaborate? – user187604 Feb 11 '19 at 00:53
  • @user187604 Say `n = 1,299,827`, a [prime](https://primes.utm.edu/lists/small/100000.txt). With `for(i=2;i<=n-1;i++)` code iterates over 1 million times. The square root of 1,299,827 is about 1,140. There is no need for the loop to iterate past 1,140 as there are no factors between 1,140 and 1,299,827 that would not have been already detected. `for(i = 2; i <= n/i; i++)` only iterates up to about `i == 1140`, that is 1000 times faster. – chux - Reinstate Monica Feb 11 '19 at 03:02
1

First off, " ... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C .." I was able to get the code to compile without that library file, so you may wish to consider removing it. As for as the code goes, well here is what I came up with:

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

int isPrime(int value) {
    int i = 2;
    for(; i < value; i++) {
        if((value % i) == 0) {
            return 0;
        }
    }
    return value > 1;
}

int main(void){

int n=0,i=0, r=0;
char * s;

printf("\nPlase enter a number to learn if it is prime:");
scanf("%d",&n);
r = isPrime(n);

printf("\n%d is ", n);
s = (r==0)?  " not a prime number" :  "a prime number";
puts(s);
return 0;
}

After the user inputs a number, the code checks whether it is prime by calling the function isPrime(), a function that returns an int. isPrime is a simple function that attempts to factor a number.

See here for similar live code that I devised.

slevy1
  • 3,797
  • 2
  • 27
  • 33