Code for finding prime numbers ( need help , code not working )

Hi all , im new to programming , i have attempted a code to find the number of prime numbers uptil a user entered number and display them.
The code isnt functioning well if i enter a number greater than 3 and doesnt display the prime numbers present , can anyone help?!!

code
___________________________________-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

using namespace std;
                        
                        // To find the prime numbers uptil an entered number
int primz=2;  // 1 and 2 by default.
int m,i;

int main()


{  int prim[m+1];
 prim[0]=0;

 cout<<"Enter the numbers till you want to find the no. of primes ";
 cin>>m;
 cout<<endl;

  for (int i=1;i<=m;i++)
   {
       prim[i]=1;   // assuming all the numbers uptil m are prime and later 
                          //eliminating
   }

if(m==1||m==0)
 {cout<<"NO PRIME NUMBER\n";
goto endz; }
if (m==2)

{
    cout<<"The prime number present is 2";
    goto endz;
}

if (m==3)
{
     cout<<"The Prime numbers present are 2 and 3";
     goto endz;
}

else

{for (i=4;i<=m;i++)

{
     for ( int k=2;k<=i-1;k++)

     {

              if(i%k==0)
              {
                  prim[i]=0; // check if the given number has any common factor other than itself and 1, and if true => not prime.
                  primz++;
               }

     }
} }


cout<<"The number of primes is "<< primz;
cout<<"The prime numbers till the required number are";

 if (prim[i]==1)
 {
     cout<<i;
 }
endz:
return 0;
}


_____________________________________________________________--
Last edited on
Two very important things about C++: 1) it is strongly suggested that you don't declare global variables (outside a function) and 2) using goto should be avoided as well.
What is the meaning of the statement on line 12? It looks like you declare an array, but what is the size? m is not defined. In addition, a lot of compilers do not like to have the size the array defined at runtime.
Please try avoiding the use of goto because it makes the code hard to read


note that 1 is NOT a prime number
int primz=2; // 1 and 2 by default.

Furthermore:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int m,i; // m and i are undefined
int main()
{  
    int prim[m+1]; // compiler should give you an error, array sizes must be const expressions

// ...
  for (int i=1;i<=m;i++)
   {
       prim[i]=1;   // might cause an out of range error or the programm just crashes
   }

// ...

                  prim[i]=0; // check if the given number has any common factor other than itself and 1, and if true => not prime.
                  primz++; // you increase the number of primes when you find a not-prime-number 


1 is a prime # because it can be only divided by 1 and itself being 1 also.
1 is a prime # because it can be only divided by 1 and itself being 1 also.
1 is definitely not a prime jason.

A prime is a natural number greater than 1 and has no positive divisors other than itself and 1.

edit:
Also, op you should look into doing a sieve of Eratosthenes.
Last edited on
1 is a prime # because it can be only divided by 1 and itself being 1 also.
Fundamental Theorem of Arithmetic: Every whole number greater than one can be written *uniquely* (except for their order) as the product of prime numbers.
1 does not fit in that concept, you can write 6 as 2*3
if 1 woule be a prime you could have 2*3*1 or 2*3*1*1*1*1*1, and it's not unique anymore
Thats why 1 is not a prime

But back to the problem, did you solve it?
Last edited on
Im totally baffled , actually i have an exam this week and i have huge syllabus to cover , can someone share a corrected code? i'd appreciate it alot!
Well, I allready did it once so I'll just share

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>

std::vector<int> primes; // currently found primes

// if rest of division is 0
bool isDivisable(int number, int divisor)
{
    return number % divisor == 0;
}
bool isPrime (int number)
{
    if(number < 2) // 0 and 1 are no primes, negative numbers are also no primes
        return false;

    int max = sqrt(number); // highest value you need to check

// check if number is divideable by any of the currently found primes that are smaller than the sqrt of number
    for (int i = 0; i < primes.size() && primes[i] <= max; ++i)
    {
        if (isDivisable( number, primes[i] )) 
        {
            return false;
        }
    }
    return true;
}
void PrintPrimes(int max)
{
    if(max < 2)
        return;

    for(int i = 0; i < max; i+=1)
    {
        if(isPrime(i))
        {
            primes.push_back(i); // when a prime is found add it to primes
            std::cout << i << std::endl; // prints the prime numbers
        }
    }
    std::cout << "primes found: " << primes.size() << std::endl; // prints the total number of primes
}

int main(void)
{
    int max;
    std::cout << "until what number do you want to count the Primes?" << std::endl;
    std::cin >> max;

    PrintPrimes(max);
    return 0;
}
Topic archived. No new replies allowed.