Is something wrong, can I get a hint?) Displays correctly only if I enter 2.

Input example

2

Sample output

12

3 4

The task:

Print the numeric square of the specified size. The displayed numbers start at one and are constantly increasing. In each line numbers are separated by spaces. Size read from the keyboard.

#include <stdio.h> int main() { int total; int temp = 1; scanf("%d", &total); printf("%d ", 1); for ( int i = 0; i < total; i++ ) { for ( int j = 0; j < i; j++ ) { temp += 1; printf("%d ", temp); } temp += 1; printf("%d\n", temp); } return 0; } 

    4 answers 4

    Must be

     ... for ( int i = 0; i < total; i++ ) { for ( int j = 0; j < total; j++ ) { temp++; printf("%d ", temp); } printf("\n"); } 

    Then total numbers in a line and total lines will be displayed. Total - total * total elements, as required.

    Or you can easily get rid of one cycle:

     for (int i = 0; i < total * total; i++) { printf("%d ", i + 1); if (((i + 1) % total) == 0) printf("\n"); // если вывели total элементов - перевод строки } 

    PS printf("%d ", 1); before cycles it is necessary to remove. This is obviously a superfluous action.

    • Thanks, if you remove printf ("% d", 1) then it starts counting from 2x, 2 3 4 5 - arcs_host
    • one
      Well, now adjust the displayed values ​​=) - gecube
    • Thanks for the help, I'll figure it out further) - arcs_host
    • If you can tell me, for example, if I mean 6 on the output I will receive 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ..... that is, how can I make the numbers under each other? - arcs_host
    • one
      To do this, you need to do two things: 1) determine how many digits will be in the longest number; 2) change the output line from printf("%d", i + 1) to printf("%5d", i + 1) . Now everything will be aligned to five characters. Another option is to transfer the number of characters to align as a separate function argument: printf("%*d", 5, i + 1) - gecube

    The inner loop is also up to total (the boundaries of both cycles should be the same, then we get a square).

    • for (int i = 0; i <total; i ++) {for (int j = 0; j <total; j ++) {temp + = 1; printf ("% d", temp); } temp + = 1; printf ("% d \ n", temp); } if so then when entering 2 displays 1 2 3 4 5 6 7 - arcs_host
    • one
      i do not need to print anything in the cycle by i - only to transfer the carriage - renegator
    • Does not fit. - arcs_host
    • printf ("\ n") - this is the carriage return - renegator
    • Thanks, I know for carriage transfer) - arcs_host

    Who cares about the final solution like this:

     #include <stdio.h> int main() { int total; int temp = 0; scanf("%d", &total); for ( int i = 0; i < total; i++ ) { for ( int j = 0; j < total; j++ ) { temp += 1; printf("%3d", temp); } printf("\n"); } return 0; } 

      Try to replace

        for ( int i = 0; i < total; i++ ) { for ( int j = 0; j < i; j++ ) { temp += 1; printf("%d ", temp); } temp += 1; printf("%d\n", temp); } 

      on

       for ( int i = 0; i < total; i++ ) { for ( int j = 0; j < i; j++ ) { temp += 1; printf("%d ", temp); } printf("\n"); } 
      • So I tried, it does not fit ( - arcs_host