Getting all prime numbers between 2 and 100 in C

若如初见. 提交于 2019-12-23 06:26:44

问题


This is my code which is supposed to output prime numbers only.

#include <stdio.h>
int prime(int n){
    int j;
    for (j=2;j<=n/2;j++){
        if((n%j)==0){
            return 0;
        }
        else{
            return 1;
        }
    }
}
void main(){
    int i,p;
    for (i=2;i<=100;i++){
        p=prime(i);
        if(p==1){
            printf("%d \n",i);
        }
    }
}

The result is 2,3,7,9,11,13,15....

not 2,3,5,7,11,13....

What did I do wrong?


回答1:


Your probably want:

int prime(int n){
    int j;
    for (j=2;j<=n/2;j++)
        if((n%j)==0)
            return 0;
   return 1;
}



回答2:


Prime Numbers are numbers which are only divisible by two numbers. eg 2,3,5,7,11 etc.

int main()
{
    int i,j,n=0;
    for(i=2;i<=100;i++)
    {
        for(j=1;j<=i;j++)
        {
            if(i%j==0)
            {
              n++;
            }
        }
        if(n==2)
        printf("%d\n",i);
        n=0;
    }
    getch();
}



回答3:


Try this :

int prime(int n){
    int j;
    int isPrime = 1;
    for (j=2;j<=n/2;j++){
        if((n%j)==0){
            isPrime = 0;
            break;
        }
    }
    return isPrime;
}



回答4:


If you're trying to find out Prime numbers up to a given number(in your case 2..100) building a prime table will speed up the process:

NOTE: it's important that inputs are sequential to help build prime table.

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

/* Building a prime table for fast look up
 * NOTE : Numbers should be input from 2 to n in a sequential pattern to help build prime table;  */

# define PRIME_TAB_MAX 200

int prime_specific(int num)
{
    static int primeTab[PRIME_TAB_MAX] = { 0, };
    static int primeCount = 0;
    int check_limit ;
    unsigned int idx;

    if (num < 2)
        return 0;

    if (primeCount > 0) {
        check_limit = (int)sqrt(num);
        for (idx = 0; idx < primeCount && primeTab[idx] <= check_limit; idx++) {
            if (0 == (num % primeTab[idx])) {
                return 0;
            }
        }
    }
    else {
        for (idx = 2; idx <= num / 2; idx++)
            if (0 == (num % idx))
                return 0;
    }

    if (primeCount < PRIME_TAB_MAX) {
        primeTab[primeCount++] = num;
        return 1;
    }
    else {
        fprintf(stderr, "\nERROR: Prime Table is full");
        return (-1); //Internal error
    }
}

int main(void)
{
    unsigned int idx;
    int status ;

    for (idx = 2; idx <= 1000; idx++) {

        status = prime_specific(idx);

        switch (status)
        {
        case 1:
            fprintf(stderr, "%d\n", idx);
            break;
        case 0:
            //Do nothing
            break;
        default:
            fprintf(stderr, "\nERROR:Internal Error");
            return (-1);
        }
    }
    return 0;
}



回答5:


Code:

#include <stdio.h>

int main ()

{

   /* local variable definition */

   int i, j;

     for(i=2; i<100; i++)
    {
      for(j=2; j <= (i/j); j++)

        if(!(i%j)) break; // if factor found, not prime

      if(j > (i/j)) printf("%d is prime\n", i);
    }

   return 0;

}


来源:https://stackoverflow.com/questions/12892360/getting-all-prime-numbers-between-2-and-100-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!