Thread: Help in the code

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    Help in the code

    Hey Guys,

    I got the following code by doing Google search. I have made some changes in this code to suit my self. It's basically for finding Lychrel numbers. The code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    long long reverse(long long i)
    {
        int rem;
        long long sum = 0;
     
        while(i > 0)
        {
            rem = i % 10;
            sum = (sum * 10) + rem;
            i /= 10;
        }
        return sum;
    }
     
    int main()
    {
        int i, count, ans = 0;
        long long next;
     
        for(i = 10; i < 10000; i++)
        {
            count = 0;
            next = reverse(i) + i;
            while(count < 50)
            {
                if(reverse(next) == next)
                {
                    break;
                }
                else
                {
                    next = reverse(next) + next;
                    count++;
                    if(count == 50)
                    {
                        ans++;
                    }    
                }
            }
        }
        printf("Answer is %d\n",ans);
    }
    I want to write the code without using BREAK or GOTO keywords, but I'm not able to figure out. If possible please help and give your inputs.

    Thanks in advance.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Replace the break with a boolean flag

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    Can you please be more specific. I didn't get what you said.
    Thanks

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by abhinav3 View Post
    Can you please be more specific. I didn't get what you said.
    Thanks
    Instead of breaking from the loop, add another condition to the loop.

    I don't think I should get more specific than that because this really smells like homework (otherwise why avoid using break?)

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    Yes, it is homework. The system that I work on doesn't support use of BREAK and GOTO statements.
    So if you could help me, it would me great.
    Thanks

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by abhinav3 View Post
    Yes, it is homework. The system that I work on doesn't support use of BREAK and GOTO statements.
    So if you could help me, it would me great.
    Thanks
    I have. Replace the break with an extra condition in the loop that stops the loop when the condition is false.

    Edit: I don't believe that using google to find solutions and modifying them is a good way, at all, to do homework. You won't learn much doing that.

    Edit:

    Code:
    while (expression) {
        /* Do stuff here */
    }
    What you have to do is instead of using break (you do know what break does, right?) you, instead, make/force expression evaluate to false. So, you have to write expression.

    Edit 2: You'll have to research and use the logical and operator (&&) or force the expression to be false in another way (which you can do, so you have two choices)
    Last edited by Hodor; 12-11-2013 at 11:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM

Tags for this Thread