So, with my program, I want to calculate prime numbers and would only like to see it come out with only one prime number. I tried to put it in a if statement at the end like "if (number_of_primes=number_tester) printf("%i/n", test_int)",
it did not work. and at it's current state it is calculating prime numbers, but it sometimes duplicates them. Also, this is in c programming.

`#include

``````#include<stdbool.h>
int main (void)
{ int number_tester, prime_number, div, number_of_primes, test_int;
printf("for which prime number do you want to know the value of?: ");
scanf("%i", &number_tester);
printf("Debug-> entered number = %i\n\n", number_tester);
prime_number=number_tester%div;
bool is_prime;
bool check_if_prime(int);
test_int=3;

if(number_tester==1)
{
printf("prime number is 2\n");
return 0;
}

for(number_of_primes=2; number_of_primes<=number_tester+1;)
{
for(div=2; div<=test_int; div++)
{
if(test_int%div==0)
{
is_prime = false;
break;
}if(test_int%div>1)
{is_prime = false;
break;
}
if(test_int%div==1)
{
is_prime=true;
number_of_primes++;
printf("%i\n", test_int);
continue;
}
}
test_int++;
}
return 0;
}``````

`

I may be wrong but the bool is_prime looks like it could be uninitialized. If you are using any current C complier turn on all warnings to see what it complains about.

Also, https://rosettacode.org/wiki/Category:Prime_Numbers has a dozen categories of work in Prime Numbers with over a thousand solutions in over 100 languages.

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.