-2

I've been trying for last 3 days to overcome the problem but I'm failing continuously.

I'm trying to print all prime numbers from 1 to 300 in C.(Dev C++)

below is the code

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

main() {
    // 1 - 300
    register int i, j;
    for (i = 1; i <= 300; i++) {
        for (j = 2; j < i; j++) {
            if (i % j == 0) {
                break;
            } else {
                printf("\n%d", i);
                break;
            }
        }
    }
}

please help me in this and also help me to clear the concept. Thanks.

ggorlen
  • 44,755
  • 7
  • 76
  • 106

7 Answers7

2

You are printing the numbers too early. Print the numbers at the end of the outer loop. ie, after the inner loop.

You could do

for(i=1; i<=300; i++)
{
    for(j=2; j<i; j++)
    {
        if(i%j == 0)
        {
            break;
        }
    }
    if(j>=i)
    {
            printf("\n%d",i);
    }   
}   

If at the end of inner loop, the condition j<i still holds, the loop was terminated prematurely due to the break statement being executed and hence the number is not prime.

But if that condition is false, ie, if j>=i is true, the number is prime.

You could also do this with the help of a flag variable.

And you probably don't need to use the register storage class specifier. See this post.


And there's no need of the break statements. In the program you've posted,

if(i%j == 0)
{
     break;
}

else
{
    printf("\n%d",i);
    break;
}

is same as

if(i%j == 0)
{
    break;
}
printf("\n%d",i);

or just

if(i%j != 0)
{
    printf("\n%d",i);
}
J...S
  • 5,079
  • 1
  • 20
  • 35
0

This should do the work for now, but I am sure that there are more efficient ways for doing this job:

int main() {
    int i, j;
    char is_prime; // indicator for each number if it is a prime number
    printf("2\n"); // first number in the series, and the only one witch is even
    for (i = 3; i<=300; i += 2) // When I jump in 2 numbers each time, I save the check of all the even numbers, that we knows ahead that they are not prime numbers.
    {
        is_prime = 1; // Start with an assumption that the number is prime. If we will find out that it doesn't, we will turn this value to 0.
        for (j = 3; j < i; j++)
        {
            if(i % j == 0)
            { // If this number fully divided by this `j` value, so it is not a prime number.
                is_prime = 0; // Turn this variable to indicate that this is not a prime number.
                break; // This loop is for testing the current number. Now we found out that it is not a prime number, so we can end this loop.
            }
        } // End of the inner loop that check if the current 'i' number is a prime number.
        if (is_prime) { // If we found that the current 'i' number is a prime number (if is_prime != 0)
            printf("%d\n", i); // Print the 'i' number.
        }
    }
    return 0;
}
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
  • @EricPostpischil I edited the answer and added some comments, do you think it is OK now? Or there is something more to do? – Coral Kashri Jul 28 '18 at 21:18
0

Generating prime numbers

This is the simplest way to achieve this

for(i=2 ; i <= n ; i++)
{
    for(j=2 ; j<i ;j++)
    {
        if(i%j == 0)
            break ; 
    }
    if(i == j )
        printf("%d\n",i);
}
0
#include<stdio.h>
void main()
{
    int n,i,fact,j;
    printf("Enter the Number");
    scanf("%d",&n);
    printf("Prime Numbers are: \n");
    for(i=1; i<=n; i++)
    {
        fact=0;
        for(j=1; j<=n; j++)
        {
            if(i%j==0)
                fact++;
        }
        if(fact==2)
            printf("%d " ,i);
    }
    getch();
}
-1

Try to solve it your self, putting values of i and j respectively, you'll find the errors.

1.Declare a variable int and initialize it by 0 (int a=0).

2.Then in the inner for loop in the if statement increase the value of a for each division.

If(i÷j==0)
{a=a+1;//or a++
 }

3.Get out of the loop now and look if the value of the a is still 0 then i is a prime else it's not!

-1

For primes up to 300, the Sieve of Eratosthenes algorithm is quite good.

FredK
  • 4,094
  • 1
  • 9
  • 11
  • Somebody who is just learning control flow is not ready to work on algorithms that require using arrays, particularly when that adds to the burden of getting the control flow correct. This answer is pitched at the wrong level. – Eric Postpischil Jul 28 '18 at 20:46
-1
#include <stdbool.h>
#include <stdio.h>
#define FIRST_POSITION_AFTER_FIVE 3
#define LAST_VALUE_OF_PREALLOCATED_ARRAY 5
#define ARRAY_SIZE 1000
#define MAX_PRIME_NUMBERS 1000
#define FIRSTS_OF_PRIME_NUMBERS 2, 3, 5

int allPrimeNumbers[ARRAY_SIZE] = {FIRSTS_OF_PRIME_NUMBERS};
int nextValue = LAST_VALUE_OF_PREALLOCATED_ARRAY;
int position = FIRST_POSITION_AFTER_FIVE;

bool isPrime(int value, int position)
{

  while (position >= 0)
  {
    if (allPrimeNumbers[position] != 0)
    {

      if (value % allPrimeNumbers[position] == 0)
      {

        return false;
      }
    }
    position--;
  }

  return true;
}

bool NextNumber()
{

  if (isPrime(nextValue, position - 1))
  {
    allPrimeNumbers[position] = nextValue;
  }

  position++;
}

void main()
{

  for (nextValue; nextValue < MAX_PRIME_NUMBERS; nextValue = nextValue + 2)
  {

    NextNumber();
  }

  return;
};