-2

I try to print loop of numbers in order For example:

123

instead i get :

1
2
3

how i can do that ?

void enterNum(int num){
    int x=1;
    for (int i=1; i<num; i++) {
        for (int z=1; z<num; z++) {
            int sum = z*x;
            NSLog(@"%i",sum);
        }
        NSLog(@"\n");
        x++;
    }
}
Mark
  • 2,380
  • 11
  • 29
  • 49
  • 2
    Your question is very unclear. What are you actually trying to get and what do you actually get? Update your question with properly formatted output that you really get and what you really want. – rmaddy Mar 05 '14 at 22:33
  • 1
    Possible duplicate from here http://stackoverflow.com/questions/3244707/nslog-without-new-line – Reza Shirazian Mar 05 '14 at 22:35
  • You could just use the C print function: `printf("%i", sum);` `printf` does not add a new line. – Milo Mar 05 '14 at 23:07

2 Answers2

2

NSLog prints every time in a new line, so you have to store the value, before printing it out. Try this:

void enterNum(int num){
    int x=1;

    // Added mutable string here
    NSMutableString *logString = [NSMutableString new];

    for (int i=1; i<num; i++) {

        for (int z=1; z<num; z++) {
            int sum = z*x;

            // Moved logging from here and appended value to logString
            [logString appendFormat:@"%i", sum];
        }

        // Log the total string here
        NSLog(@"%@\n", logString);

        // Clear the logString
        [logString setString:@""];
        x++;
   }
}
0

You could just use the C print function: printf("%i", sum); printf does not add a new line. The downside to this is that it doesn't print Objective - C objects i.e. you cannot use %@.

Milo
  • 5,041
  • 7
  • 33
  • 59