1

The following program calculates and removes the remainder of a number, adds the total of the remainders calculated and displays them.

#import <Foundation/Foundation.h>
int main (int argc, char * argv[]) {
    @autoreleasepool {
        int number, remainder, total;
        NSLog(@"Enter your number");
        scanf("%i", &number);

        while (number != 0)
        {
            remainder = number % 10;
            total += remainder;
            number /= 10;
        }
        NSLog(@"%i", total);    
    }
    return 0;
}

My questions are:

  1. Why is the program set to continue as long as the number is not equal to 0? Shouldn't it continue as the long as the remainder is not equal to 0?

  2. At what point is the remainder discarded from the value of number? Why is there no number -= remainder statement before n /=10?

[Bonus question: Does Objective-C get any easier to understand?]

5
  • What is the purpose of this program? It's doing a simple and completely arbitrary calculation to what I can tell.. Commented Aug 30, 2012 at 22:11
  • 5
    This isn't objective-c, this is C. Commented Aug 30, 2012 at 22:12
  • Why would be there a number = remainder? Commented Aug 30, 2012 at 22:15
  • 2
    All of your questions can be answered by simply stepping through that code in the debugger. Commented Aug 30, 2012 at 22:15
  • And the initialisation of total? What I see here is just a program which sum number which compose a number? Commented Aug 30, 2012 at 22:16

2 Answers 2

2
  1. The reason we continue until number != 0 instead of using remainder is that if our input is divisible by 10 exactly, then we don't get the proper output (the sum of the base 10 digits).

  2. The remainder is dropped off because of integer division. Remember, an integer cannot hold a decimal place, so when we divide 16 by 10, we don't get 1.6, we just get 1.

And yes, Objective-C does get easier over time (but, as a side-note, this uses absolutely 0 features of Objective-C, so it's basically C with a NSLog call).

Note that the output isn't quite what you would expect at all times, however, as in C / ObjC, a (unlike languages like D or JS) a variable is not always initialized to a set value (in this case, you assume 0). This could cause UB down the road.

Sign up to request clarification or add additional context in comments.

4 Comments

@JodyHagins the autoreleasepool does nothing in this scenario, as there is nothing to autorelease. The NSStrings are constant, and they are the only objects around.
@Richard J. Ross III Just beat me to it... +1 for the initialization to zero. Very good point.
Just noting that autoreleasepool it is not C (nor the import for that matter :-)
I completely forgot about integer division! Thank you.
0
  1. It checks to see if number is not equal to zero because remainder very well may never become zero. If we were to input 5 as our input value, the first time through the loop remainder would be set to 5 (because 5 % 10 = 5), and number would go to zero because 5 / 10 = 0.5, and ints do not store floating point values, so the .5 will get truncated and the value of number will equal zero.
  2. The remainder does not get removed from the value of number in this code. I think that you may be confused about what the modulo operator does (see this explanation).

Bonus answer: learning a programming language is difficult at first, but very rewarding in the long run (if you stick with it). Each new language that you learn after your first will most likely be easier to learn too, because you will understand general programming constructs and practices. The best of luck on your endeavor!

1 Comment

Between you and Richard I completely understand, thank you in particular for your explanation of question 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.