Thread: help on loop

  1. #46
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because your while-loop is wrong. You need two loops, one to repeat the input of numbers and all the code to check if the number is a palindrome and another one that covers the three lines that reverses the number. Currently, you are copying the last digit of a (or n) into b, but the remaining digits are still in a. You need to continue moving digits from a to b until there are no digits left in a.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #47
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What with including <iostream.h>? That's from pre-standard C++, and this is C. You got it right earlier by including <stdio.h>.

    Take a good look at the code below:
    Code:
    #include <stdio.h>
    
    int isPalindrome(int n);
    
    int main()
    {
        int n;
    
        printf("Enter a five-digit number ( -1 to end ): \n");
        scanf("&#37;d", &n);
    
        while (n != -1)
        {
            if (isPalindrome(n))
            {
                printf("%d is a palindrome\n", n);
            }
            else
            {
                printf("%d is not a palindrome\n", n);
            }
    
            printf("Enter a five-digit number ( -1 to end ): \n");
            scanf("%d", &n);
        }
    
        return 0;
    }
    
    /* Return 1 if n is a palindrome, else return 0. */
    int isPalindrome(int n)
    {
        /* ... */
    }
    It is basically your current code, except that it is well indented, includes the correct header, uses int main() instead of void main(), corrects the loop condition (you forgot to assign n to a, but with such undescriptive variable names, it is hard to know what is a and b anyway), shifts the duplicated code out of the if and else blocks, and... uses a function to determine if the number is a palindrome. Now you need to implement that function.

    Right, so I have been pretty generous with the above, and I hope that at the very least in your future code you will indent your code properly and use descriptive names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #48
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Psh... I don't see what the problem was to my approach. And since I used 64-bit numbers you can even have reasonably long numbers before buffer overflows come out to play.

  4. #49
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Thumbs up

    i have finaly make this program work the way i wanth.. thanks guys...

    this is the code that i have used..

    Code:
    #include <stdio.h>
    
    void main()
    {
    int a , b, c, d;
    int n;
    int temp;
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("%d",&n);
    
    while(n!=-1)
    {
    a =n /10000;
    temp=n%10000;
    b =temp /1000;
    temp=n % 100;
    c=temp/10;
    temp=n%10;
    d =temp/1;
    if(a == d && b == c)
    {
    printf("%d is a palindrome\n" , n);
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("%d",&n);
    }
    else
    {
    printf("%d is not a palindrome\n" , n);
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("%d",&n);
    }
    }
    }

  5. #50
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's going to be a long road. 20 posts, and you're still using void main, and you still can't indent code at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #51
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Thumbs up

    Quote Originally Posted by Salem View Post
    It's going to be a long road. 20 posts, and you're still using void main, and you still can't indent code at all.
    what?? dont get you.. i said i have made the program work the way i wan it..

  7. #52
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean the way your current compiler lets you get away with.

    Sooner or later, you'll change compiler. Only then will you discover that the slap-happy "works for me" approach was a folly. Been there, done that. If you want the T-shirt as well, then that's up to you.

    Your code is already at the limits of understandability without any indentation. If it were 10 times longer, people would just ignore you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #53
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Quote Originally Posted by StormRoBoT View Post
    i have finaly make this program work the way i wanth.. thanks guys...

    this is the code that i have used..

    Code:
    #include <stdio.h>
    
    void main()
    {
    int a , b, c, d;
    int n;
    int temp;
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("&#37;d",&n);
    
    while(n!=-1)
    {
    a =n /10000;
    temp=n%10000;
    b =temp /1000;
    temp=n % 100;
    c=temp/10;
    temp=n%10;
    d =temp/1;
    if(a == d && b == c)
    {
    printf("%d is a palindrome\n" , n);
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("%d",&n);
    }
    else
    {
    printf("%d is not a palindrome\n" , n);
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("%d",&n);
    }
    }
    }
    int main (void)
    {
    int a, b, c, d;
    int n;
    int temp;

    do
    {
    printf("Enter a five-digit number < -1 to end >: ");
    scanf(" %d", &n); // if you are going to ask the user for input over and over again, it is always good to clear the input stream.

    a = n / 10000;
    temp = n % 10000;
    b = temp / 1000;
    temp = n % 100;
    c = temp/10;
    temp = n%10;
    d = temp/1;

    if(a == d && b == c)
    printf("%d is a palindrome\n", n);
    else
    printf("%d is not a palindrome\n" , n);

    while (n != -1); // as long as n is NOT -1, the loop continues.

    return 0;
    }

    I believe this code would do a better job than yours, you don't have to use scanf and printf over and over again. Just use a do while loop and set your condition to check whether n is NOT -1.

  9. #54
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I believe this code would do a better job than yours, you don't have to use scanf and printf over and over again. Just use a do while loop and set your condition to check whether n is NOT -1.
    Use code tags for posting code

    1. What will happen to your code if user enters string instead of number?
    2. What will happen to your code if user enters 6-digit number?
    3. etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread