-5

its printing 206 , where it is going wrong ?

#include<stdio.h>
#include<conio.h>

void main()
{
    int n ,c,m;
    n=100;
    c=0;

    while(n<1000)
    {
       for(m=2;m<10;m++)      //count c  for non prime numbers
       {
           if(n%m==0)
           {
               c++;
               m=100;
           }
       }
       n++;
    }
    printf("%d", 900-c);
    getch();
}
Steve
  • 7,171
  • 2
  • 30
  • 52
MINCH
  • 1
  • Your code checks whether the number in question is divisible by numbers from 2 to 10? Is that your definition of prime? – Michael Krelin - hacker Oct 23 '13 at 19:10
  • Your code looks weird. But `m` should run up to `sqrt(1000)`, or about `31`. You are checking only up to `10`. – mvp Oct 23 '13 at 19:13
  • If I've understood this, you're counting the number of integers between 2 and 10 that **will** divide exactly into each number from 100 to 1000. –  Oct 23 '13 at 19:14
  • *is printing 206*... what answer do you believe it's supposed to give and why? – lurker Oct 23 '13 at 19:15
  • thought that if a number 100<=n<1000 is divisible by any number between 2 to 9 then its non prime and so c counts those numbers , and at last 900-c shoud give no. of primes . – MINCH Oct 23 '13 at 19:21

1 Answers1

0

Your code has some serious design flaws. Let's start from scratch:

The most efficient way (as far as I know) of dumping out a series of prime numbers in a certain range whose lower bound is close to zero is to use a Sieve of Eratosthenes algorithm. See

Sieve of Eratosthenes algorithm in C

Note that this is a different problem to asking whether or not a given number is prime: that's often solved by iterating through the primes up to and including the square root of the number to test.

Community
  • 1
  • 1
Bathsheba
  • 231,907
  • 34
  • 361
  • 483