C For Loop Floyd's Triangle but different?

102 views Asked by At

I need help on how to do a Flyod's Triangle style but instead of input value in rows, the triangle is based on the input value as a whole.

instead of;

Enter a number: 9
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45

it should be

Enter a number: 9
1
23
456
789

here is my code

#include <stdio.h>
int main() {
   int rows, i, j, number = 1;
   printf("Enter a number: ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; i++) {
      for (j = 1; j <= i; ++j) {
         printf("%d ", number);
         ++number;
      }
      printf("\n");
       
   }
   return 0;
}
2

There are 2 answers

1
john_hatten2 On
#include <stdio.h>
int main() {
   int max_num, i, j, number = 1;
   printf("Enter a number: ");
   scanf("%d", &max_num);
   for (i = 1; 1; i++) {
      for (j = 1; j <= i; ++j) {
         printf("%d ", number);
         ++number;
         if (number > max_num) {
             return 0;
         }
      }
      printf("\n");
       
   }
   return 0;
}
0
Aconcagua On

To easily structure your output you might retain a row counter; in contrast to your code it only serves to determine how many columns we need per row, so:

for(size_t row = 1; ; ++row) // note: no condition; we'll break from within!
{
    for(size_t col = 0; col < row; ++col)
    {
       // we'll print the values here!
    }
}

This is the basic structure how to define rows and colums.

Now you'll input before this loop the target number up to which to output values, and you'll have a separate counter running up to the number just received by the user, i.e.:

int number;
if(scanf("%d", &number) != 1) // test to catch invalid input, though it does not
                              // catch all types of, e.g. for 77xyz the value 77
                              // will be scanned!
{
    // TODO: error message!
}
else if(number < 0)
{
    // again invalid input!
    // TODO: error message
}
else if(number == 0):
{
    // special case, don't output anything
}
else
{
    int counter = 1;

    // now our loops:
    for(size_t row = 1; ; ++row) 
    {
        for(size_t col = 0; col < row; ++col)
        {
            printf("%.2d ", counter);
            // .2 is optional, it serves for better alignment of the output
            // though fit ails for number > 99; you might calculate *before*
            // this nested loop how many indentation you actually need...

            // and now we check if we need to stop!
            if(counter == number)
            {
                // as within main, we can just return; maybe add another
                // newline before:
                return 0;
            }

            ++counter; // go on with next number
        }
        putchar('\n');
    }
}

Note how the loop counters don't have any influence on stopping the output, only our explicit counter variable has.

Note, too, that above code is untested, so if you find a bug, please fix yourself.