Good Afternoon,
I am trying to create a program that generates all the prime numbers from 2 to n, an intiger input by the user. I tried to accomplish this by using a function that i created by the name is_prime:
I want to include a counter in my program so that the final output looks something like this:
1: 2
2: 3
3: 5
4: 7
5: 11
6: 13
7: 17
etc.
I would really appreciate some help with the actual function.
Thanks in advance.
I am trying to create a program that generates all the prime numbers from 2 to n, an intiger input by the user. I tried to accomplish this by using a function that i created by the name is_prime:
Code:
#include <stdio.h>
#include <stdlib.h>
int is_prime(int n)
{int k, limit;
if (n == 2)
return 1;
if (n % 2 == 0)
return 0;
limit = n / 2;
for ( k = 3; k <= limit; k += 2)
if (n % k == 0)
return 0;
return 1;}
1: 2
2: 3
3: 5
4: 7
5: 11
6: 13
7: 17
etc.
I would really appreciate some help with the actual function.
Thanks in advance.
Comment