-1

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]);
  }
}


chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • `for(i=0;i<=50;i++){` --> `for(i=0;i<50;i++){` – David Ranieri Dec 13 '22 at 16:25
  • Three things: What happens if not all elements of the array `primenum` are initialized, what will you print for values then? Secondly, an array of `50` elements will have indexes from `0` to **`49`**, while your code will use index `50` which is out of bounds. Lastly, what happens if there are more than `50` prime numbers in the range? – Some programmer dude Dec 13 '22 at 16:25
  • your if will not work – 0___________ Dec 13 '22 at 16:26
  • 1
    Oh, and you will increase `primeindex` to many times. – Some programmer dude Dec 13 '22 at 16:27
  • 4
    The lesson for today should be to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. Like for example how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code line by line while monitoring variables and their values. – Some programmer dude Dec 13 '22 at 16:29
  • Problem is certainly in `a = i / primeindex; for (j = 2; j <= a; j = j + 1) {` and `i <= 50`. – chux - Reinstate Monica Dec 13 '22 at 16:32
  • well then how should i solve the problem can anyone send me the corrected code please – Tanay Mithari Dec 13 '22 at 16:33

2 Answers2

1

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

0___________
  • 60,014
  • 4
  • 34
  • 74
0

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;
}
Fe2O3
  • 6,077
  • 2
  • 4
  • 20