Check wheather the entered number is prime also repeat it

This is a discussion on Check wheather the entered number is prime also repeat it within the C Programming forums, part of the General Programming Boards category; I'm Rajesh. I'm new to coding. I'm using 'Let us C' book to learn C myself. So I need help ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    11

    Question 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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    11

    Exclamation 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. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Because it removes the said newline from the buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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!

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    Thanks Adak. Your reply was useful.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    If you use scanf for a number, that newline still gets stuck in the buffer since it cannot be converted to a number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime number check
    By password in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2007, 10:30 AM
  2. Prime Number stops after 29, but why?
    By Daveo in forum C Programming
    Replies: 22
    Last Post: 09-17-2004, 10:55 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. How is to check prime number?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-04-2001, 11:36 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21