i am having trouble fixing this program... its not printing the number any tips???
i am using sieve of Eratosthenes

``````#include <cstdio>
#include "simpio.h"
#include "strlib.h"
#include <iostream>
using namespace std;

int main()
{
bool is_prime[300];
int num[300];
int i, n;

printf("This program prints all prime numbers from 1 to 300.\n");

// Identifing the numbers
for (i = 0; i<=300; i++)
{
is_prime[i] = true;
num[i] = i;
}
is_prime[0] = false;
is_prime[1] = false;

//Identifing the non prime
for(i = 1; i<=300; i++)
{
for(n = i; n<=300;)
{
is_prime[n] = false;
n = n*2;
}
}
//Displaying numbers
for(n = 0; n<=300; n++)
{
if (is_prime[n] == true)
{
printf("%d\n",num[n]);
}
}
//Pausing program
system("pause");
}``````

## All 8 Replies

That you're stepping off the ends of your arrays would be one problem.

???

> for (i = 0; i<=300; i++)
Arrays run from 0 to N-1
So it's < 300, not <= 300

that doesn't solve the problem its still list every number from 2 to 299

Well the next step would be to go read up on how the sieve actually works.
Because your code looks nothing like it.

For a start, the outer loop should stop at the square root of 300.

shit i missed something big!

``````#include <cstdio>
#include "simpio.h"
#include "strlib.h"
#include <iostream>
using namespace std;

int main()
{
bool is_prime[300];
int num[300];
int i, n;

printf("This program prints all prime numbers from 1 to 300.\n");

// Identifing the numbers
for (i = 0; i<=300; i++)
{
is_prime[i] = true;
num[i] = i;
}
is_prime[0] = false;
is_prime[1] = false;

//Identifing the non prime
for(i = 1; i<=300; i++)
{
for(n = i; n<=300;)
{
is_prime[n] = false;
n = n*2;
}
}
//Displaying numbers
for(n = 0; n<=300; n++)
{
if (is_prime[n] == true)
{
printf("%d\n",num[n]);
}
}
//Pausing program
system("pause");
}``````

Why do you keep posting these aberrations of C++ in the C forum?

``````#include <cstdio>
#include <iostream>
using namespace std;``````

Those are header files and syntax that belongs to C++
And so does this:

``bool is_prime[300];``

C language do not have a key word bool.
Very likely you are compiling a .cpp file.

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

//*************************************************
//skips even numbers and only checks squared values
//reduces the loop size runtime
//*************************************************

int IsPrime(long number);

int main(int argc, char *argv[]){
long n;
printf("Enter number to check for primality:");
scanf("%ld",&n);
if(IsPrime(n)) {
printf("Number is a prime\n");
} else {
printf("Number is not a prime\n");
}

int c;
printf("Press any key to continue..");
c = getch();
}

int IsPrime(long number) {
long i;

i =2;
if (number % i == 0) return 0;

for (i=3; (i*i)<=number; i+=2) {
if (number % i == 0) return 0;
}
return 1;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.