-3

In C, I need to print the number pattern in the below manner in right alignment, ie: when the double digit comes the upper single digit should adjust itself to right and so on.

int main() {
    long int k = 1;
    int i, j, n;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            printf("%ld ", k);
            k++;
        }
        printf("\n");
    }
    return 0;
}

required output:

 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 
46 47 48 49 50 51 52 53 54 55 ` 

output I get:

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 
46 47 48 49 50 51 52 53 54 55 

output required:

  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 
 46  47  48  49  50  51  52  53  54  55 
 56  57  58  59  60  61  62  63  64  65  66 
 67  68  69  70  71  72  73  74  75  76  77  78 
 79  80  81  82  83  84  85  86  87  88  89  90  91 
 92  93  94  95  96  97  98  99 100 101 102 103 104 105 
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 

output I get:

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 
46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 
67 68 69 70 71 72 73 74 75 76 77 78 
79 80 81 82 83 84 85 86 87 88 89 90 91 
92 93 94 95 96 97 98 99 100 101 102 103 104 105 
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Siri
  • 17
  • 4
  • Siri, show us your code and we will help you fix it. – nicomp Jan 21 '18 at 14:53
  • 2
    Read the documentation for `printf()`. – Code-Apprentice Jan 21 '18 at 14:53
  • This is a display of 0 effort work – QuIcKmAtHs Jan 21 '18 at 14:53
  • [does this help?](https://stackoverflow.com/q/40575096/2173917) – Sourav Ghosh Jan 21 '18 at 14:56
  • Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Sourav Ghosh Jan 21 '18 at 14:57
  • Your code works fine. What error did you get? – QuIcKmAtHs Jan 21 '18 at 15:03
  • 1
    i need right alignment of numbers as in the above patterns observe that the single digits moved right when a double digit came.... – Siri Jan 21 '18 at 15:05

3 Answers3

1

You should calculate the length of the last outputted number and use the value as the field width in a printf call..

Here you are.

#include <stdio.h>

int main(void) 
{
    while ( 1 )
    {
        printf( "Enter a non-negative number (0 - exit): " );
        unsigned int n;

        if ( scanf( "%u", &n ) != 1 || n == 0 ) break;

        unsigned int upper = n * ( n + 1 ) / 2;

        int size = 0;

        do { ++size; } while ( upper /= 10 );

        putchar( '\n' );

        for ( unsigned int i = 0, value = 0; i < n; i++ )
        {
            do
            {
                printf( "%*u ", size, ++value );
            } while ( value != ( i + 1 ) * ( i + 2 ) / 2 );

            putchar( '\n' );
        }

        putchar( '\n' );
    }

    return 0;
}

The program output is

Enter a non-negative number (0 - exit): 10

 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 
46 47 48 49 50 51 52 53 54 55 

Enter a non-negative number (0 - exit): 20

  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 
 46  47  48  49  50  51  52  53  54  55 
 56  57  58  59  60  61  62  63  64  65  66 
 67  68  69  70  71  72  73  74  75  76  77  78 
 79  80  81  82  83  84  85  86  87  88  89  90  91 
 92  93  94  95  96  97  98  99 100 101 102 103 104 105 
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 

Enter a non-negative number (0 - exit): 0
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

I will not help you write a code because zero effort is shown, but I can give hints via a pseudo code.

cin >> n; //desired number of rows
for (int i = 0; i < n; i++){
    //generate triangle numbers
}//store the first n triangle numbers, given by n(n-1)/2
for (int i = 0; i < n; i++){
    cout << i;
    if (i==any triangle number){
        cout << "\n"; //note that an endline is printed only at triangle numbers
    }
}
QuIcKmAtHs
  • 297
  • 4
  • 18
  • i did a code but i'm unable to get the alignment as required int main() { long int k=1; int i,j,n; scanf("%d",&n); for(i=1;i<=n;i++){ for(j=1;j<=i;j++){ printf("%ld ",k);k++; }printf("\n"); } return 0; } – Siri Jan 21 '18 at 15:00
  • 1
    include that in your post please. – QuIcKmAtHs Jan 21 '18 at 15:01
-1

Here is a simple solution that handles end of lines correctly too:

#include <stdio.h>

int main(void) {
    int i, j, k, n, len;
    if (scanf("%d", &n) == 1 && n > 0) {
        /* compute the width of the last number */
        //len = snprintf(NULL, 0, "%d", n * (n + 1) / 2);
        //char buf[48]; /* large enough for 128-bit integers */
        //len = sprintf(buf, "%d", n * (n + 1) / 2);
        for (len = 1, k = n * (n + 1) / 2; k > 9; k /= 10)
            len++;
        for (i = k = 1; i <= n; i++) {
            for (j = 1; j < i; j++) {
                printf("%*d ", len, k++);
            }
            /* print the last number on the line without a trailing space */
            printf("%*d\n", len, k++);
        }
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Yes, using `sprintf` and a local array, I shall amend the answer for portability to legacy systems with poor support for C99, an 18 year old standard. – chqrlie Jan 21 '18 at 16:05
  • Alternately, you can use a loop as in Vlad's answer. – chqrlie Jan 21 '18 at 16:14