Home > Back-end >  I was creating the program for generating prime number between 1 to 100 but it not working how it sh
I was creating the program for generating prime number between 1 to 100 but it not working how it sh

Time:12-14

I was creating the program for generating prime number between 1 to 100 where I have already stored the first and second element of the array manually and I use the for loop to generate the next odd number and then I divided the number generated with integer 2 which I stored it in variable primeindex e.g.=9/2= 4.5 and the division gets stored in variable a so the value of a is 4.5 for this loop and then I nested another for loop to check the number is prime or not by generating a number from 2 to number stored in variable a and then dividing the odd number generated in the main for loop by the the number generated by second for loop and checking if the reminder is 0 or not for each loop "e.g. 9/2 has reminder 1, 9/3 has reminder 0" and if the reminder is 0 then the odd number generated gets skipped and the next odd number is generated and if the reminder of that number is not 0 then it gets stored in array and the loops goes on until the the program finds all the primenumber till 100 and it prints it out but it not working as its intended like can some one tell me what's the problem here.

#include<stdio.h>
#include<stdlib.h>

int main() {
  int primenum[50];
  int i, j;
  int a;
  int primeindex = 2;

  primenum[0] = 2;
  primenum[1] = 3;
  //generating the number
  for (i = 5; i <= 100; i = i   2) {

    //checking if the number is nirme number or not
    a = i / primeindex;
    for (j = 2; j <= a; j = j   1) {
      if (i % j == 0) {
        break;
      } else
        primenum[primeindex] = i;
        primeindex;
    }
  }

  //printing out the array
  for (i = 0; i <= 50; i  ) {
    printf("\n\n%d", primenum[i]);
  }
}


CodePudding user response:

You if is wrong.

You can use your found prime numbers to find the next ones.

int main(void){
    int primenum[50] = {0};
    int i,j;
    int a;
    int primeindex=2;
    int isprime;

    primenum[0]=2;
    primenum[1]=3;
    for(i=5;i<=100;i=i 2){
        isprime = 1;
        for(j = 0; j < primeindex; j  )
        {
            if(i % primenum[j]==0)
            {
                isprime = 0;
                break;
            }
        }
        if(isprime) 
        {
            primenum[primeindex]=i;
              primeindex;            
        }
    }

    //printing out the array
    for(i=0;i<primeindex;i  ){
        printf("%d\n\n",primenum[i]);
    }
}

https://godbolt.org/z/z5P1Wa4f8

CodePudding user response:

Revising the answer provided by @O_____, here's an alternative solution that will fill-to-capacity the array set aside for storing primes. Instead of setting an upper limit for the value to be tested, this approach generates the first 'n' primes. Easy enough to modify to generate the first 42 prime numbers. (And using an unsigned variable doubles the range of values that can be tested.)

int main( void ) {
    uint32_t primes[ 25 ] = { 2 }; // arbitrary "25", seeded with first prime number
    size_t idx = 1;
    const size_t nPr = sizeof primes/sizeof primes[0];

    for( uint32_t i = 3; idx < nPr; i  = 2 ) { // Start with 3, testing only odd's
        size_t t = 1; // don't test if the odd values are divisible by '2'
        while( t < idx && i % primes[t] ) t  ;
        if( t == idx )
            primes[ idx   ] = i;
    }

    for( size_t j = 0; j < idx; j   )
        printf( "%d\n", primes[ j ] );

    return 0;
}
  • Related