# Thread: Prime number program problem

1. ## Prime number program problem

Hello,

My program has to determine if a number that I enter is a prime number - which I believe I have done. But if the number is not a prime number, it should tell me the highest divisor of this particular number. At the moment, my program gives all the divisors if the number is not a prime. And the input range must be between 1 and MAXINT. Can anyone tell me how to get MAXINT? how to declare it?

Thanks

Code:
```#include <stdio.h>
#include <process.h>

void main()
{
char  your_name[12];
int   number;
int   i;
int   remainder;
int   not_divisible = 1;

scanf("%s", your_name);
printf("Enter a number: ");
scanf("%d", &number);

printf("\nThe number you entered is: %d", number);
printf("\n");

for( i = 2; i < 10; i++ )
{
remainder = number % i;
if(remainder == 0 )
{
not_divisible = 0;
printf("\This number is divisible by %d", i);
printf(" and it is not a prime number");
}
}
if(not_divisible == 1) printf("This a prime number!");
printf("\n");
printf("Good-bye %s", your_name);
printf("\n");
system("pause");
}```

2. If you're referring to the one in stddef.h then you don't need to define it. It's called INT_MAX and you can use it anywhere in your program.

If you'd like to define your own then somewhere in your program (near the top) type #define MAXINT 5 or whatever you want MAXINT to be.

3. Originally Posted by itsme86
If you're referring to the one in stddef.h then you don't need to define it. It's called INT_MAX and you can use it anywhere in your program.
I believe you mean limits.h

4. Thanks for the tip, however, I just saw an example where value.h is declared in order to use MAXINT
And can anyone help me out on how I can just display the highest divisor instead of all of them?

Thanks,

5. Real quick:
Code:
```#include <stdio.h>
#include <process.h>

int main(void)
{
char  your_name[12];
int   number;
int   i;
int   remainder;
int   not_divisible = 1;
int   highest_div=1;

scanf("%s", your_name);
printf("Enter a number: ");
scanf("%d", &number);

printf("\nThe number you entered is: %d", number);
printf("\n");

for( i = 2; i < 10; i++ )
{
remainder = number % i;
if(remainder == 0 )
{
not_divisible = 0;
highest_div = i;
}
}
if(not_divisible == 1) printf("This a prime number!");
else
printf("This number is divisible by %d and thus is not prime\n", highest_div);

printf("\n");
printf("Good-bye %s", your_name);
printf("\n");
system("pause");
}```

6. Oops! limits.h, sorry. That's what I get for not double-checking.

7. Originally Posted by Guti14
Thanks for the tip, however, I just saw an example where value.h is declared in order to use MAXINT
And can anyone help me out on how I can just display the highest divisor instead of all of them?

Thanks,
Code:
```#include <stdio.h>

int main(void) // main() returns an int - always
{
char your_name[12];
int number;
int i;
int remainder;
int not_divisible = 1;

scanf("%s", your_name);
printf("Enter a number: ");
scanf("%d", &number);

printf("\nThe number you entered is: %d", number);
printf("\n");

for (i = 2; i < 10; i++) {
remainder = number % i;
if (remainder == 0) {
not_divisible = 0;
}
}

/* Get your printf() statements out of the for loop */

printf("\nThis number is divisible by %d", i);
printf(" and it is not a prime number");

if (not_divisible == 1)
printf("This a prime number!");
printf("\n");
printf("Good-bye %s", your_name);
printf("\n");

return 0;

}```
And value.h is not part of the C standard library

edit::

ack - the above only is to show how to display one result instead of all of them.

8. It's funny how I was taught with void main() and why is it a bad idea to have printf in a loop?
I think I need a good tutorial, can you direct me to a website?

9. How about the FAQ on this very same website? http://faq.cprogramming.com/cgi-bin/...&id=1043284376

EDIT: Here's a key quote:
What's the deal with void main()

Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:

void foo(void);

A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.

Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.

10. Originally Posted by kermit
Code:
```    for (i = 2; i < 10; i++) {
remainder = number % i;
if (remainder == 0) {
not_divisible = 0;
}
}```
This isn't right.

Actually you want to go to number - 1, not some fixed maximum. For example, try this with 5, and your program will see that 5 is divisible by 5, so it will erroneously set "not_divisible" to 0, and when other logic is fixed, it will report that 5 is not a prime.

Also to find the biggest divisor, you want something like this:

Code:
`int biggest = 1;`
at the beginning, then make the loop look like

Code:
``` for (i = 2; i < number - 1; i++) {
remainder = number % i;
if (remainder == 0) {
biggest = i;
}
}```
So that the variable "biggest" holds the largest divisor found as it steps through the loop.

Then, outside the loop,

see whether (biggest == 1). If it is, this is a prime number (by definition), otherwise the largest divisor is equal to the value of "biggest". So then print the appropriate message and say goodnight.

A final note: you really need to protect the program from people with long names. What if someone enters a name with 12 or more characters? Your array is only good for 11 characters (+ the terminating '\0').

Dave

11. If you're looking for the highest divisor, then why not just count backwards
in your loop. Something like this:
Code:
```#include <stdio.h>
#include <math.h>

int isPrime ( int n )
{
int i;

for (i = n/2; i > 1; i--) {
if ((n % i) == 0) {
break;
}
}

printf("Largest divisor = %d\n", i);

return (i == 1);
}

int main ( void )
{
int n = 11;

if (isPrime(n)) {
printf("%d is prime\n", n);
}

return 0;
}```

12. Originally Posted by Guti14
It's funny how I was taught with void main() and why is it a bad idea to have printf in a loop?
I think I need a good tutorial, can you direct me to a website?
It is not a bad idea to use a printf() statement in a for loop, as long as that is what you need. You had written this:

And can anyone help me out on how I can just display the highest divisor instead of all of them?
In your case, your printf() statements were inside the for loop, so they executed every time through the loop, as opposed to just once, if they were outside of the loop. Of course, as Dave pointed out, there were other problems with your code, so that you could not find a prime number.

Another site you might want to look at for a tutorial can be found here.

~/