# Thread: Check wheather the entered number is prime also repeat it

1. ## Check wheather the entered number is prime also repeat it

I'm Rajesh.
I'm new to coding.
I'm using 'Let us C' book to learn C myself.
So I need help from experts.
Please check code below. I have mentioned the problem too below.

Code:
```/*Check wheather the entered number is prime.*/
#include<stdio.h>
#include<math.h>
main( )
{
int mod, i, num, div;
char repeat='y';
while (repeat=='y')
{
printf("Enter the number:");
scanf("%d",&num);
div=pow(num,0.5);
/*printf("%d",div);*/
for (i=2;i<=div;i++)
{
mod=num%i;
if(mod==0)
{
printf("%d is not a prime number\n",num);
break;
}
}
if(mod!=0)
printf("%d is a prime number\n",num);
printf("Do you want to enter another number?(y/n):");
scanf("%c",repeat);
}
}```
Above is the code I wrote.
The programming in running once correctly, but it's not looping. ie., it is not even giving me an option to type y/n. Can someone help me for this?

Thanks,
Rajesh.

2. The problem is a trap that always get's new C programmers. The newline '\n'.

After the scanf("%d", &num), you still have a newline char left, in the keyboard buffer. Numbers don't care with scanf(), because they just skip over whitespace and newlines.

But your scanf("%c", repeat), will NOT. It pulls the newline off the keyboard buffer, and says "OK, I've got my char", and never stops.

Add this line just before the scanf("%c", repeat):

Code:
`getchar();`
and your newline problems will be gone. Does repeat need an ampersand in your scanf("%c", &repeat) ?

3. ## Thanks - 1 more question.

I added this line as u said.

Code:
`getchar();`
That was not alone the problem. I accidentally found that I forgot to add & in the last scanf statement. But your clue was very useful.
My question is why is that getchar line needed here? What is the use of it in this place. I don't see any refernce for that in my book. That's y I'm asking you.

Thanks,
Rajesh.

4. Because it removes the said newline from the buffer.

5. Oh, I know!

Think of the newline like a rubber ducky, floating with the water, down the stream that is your keyboard buffer.

If you scanf() for a number, or for a string next- then no problem, the rubber ducky gets out of the keyboard buffer stream, and you're next scanf() is good to go.

But when you scanf() for a char, the rubber ducky falls into the output bucket and the blind bucket says "I've got a fish!", and the buckets stop dipping in the stream.

Book authors tend to gloss over this fact in their products. You can not afford to ignore it.
scanf() looks like an innocent input function - it is not. It was designed for FORMATTED input, by programmers and their data. It is complex, with *many* options and pitfalls. Use it now, because it's simple, and you need to use something for input. Later, get rid of it in favor of better input functions that you will learn about, for user input.

This newline subject is so common with new programmers, it's obvious that the problems of scanf() are not being taught very well.

Code on!