简体   繁体   中英

C program to print right angle triangle

I am trying to write a program in C using for loops which will output a pattern similar to this:

 1
 2 3
 4 5 6
 7 8 9 10

As you can see the pattern is a right angled triangle with each row with the same amount of numbers as the first number in each row. I am a beginner in C and chose to go about solving to practice my use of for loops and done this step by step so i chose to first find out how i can print the first column. Here is my code so far:

#include <stdio.h>

int main()
{
   int n;
   int p = 1;

   printf("enter number of rows: ");
   scanf("%d", &n);

   for (int i= 1; i <= n; i += p++){
      printf("%d\n", i);
   }

   return 0;
}

What i found was that the first number of each column was being increased by 1 when broken down for example ( 1 + 1 = 2 , 2 + 2 = 4 , 4 + 3 = 7 ) As you can see each number i have made bold makes up the first column. My problem is that when i ask the use to enter a value to make up a specific number of rows, such as for example 5, the for statement prints out only 4 rows as you can see do below:

enter number of rows: 5                                      
1                                                            
2                                                            
4

I am guessing this is due to the statement i <= n but how do i specify the amount of rows without disrupting the calculation of the first column?

Here you have a triangle printing program.

int ndigits(unsigned long long x)
{
    int ndg = 0;
    while(x)
    {
        ndg++;
        x /= 10;
    }
    return ndg;
}

void triangle(int rows)
{
    int count = 1;
    int ndg = ndigits(rows * (rows + 1) /2);

    for(int row = 1; row <= rows; row++)
    {
        for(int column = 1; column <= row; column++)
        {
            printf("%0*d ", ndg, count++);
        }
        printf("\n");
    }
}

int main(void)
{
    triangle(30);
}

the function ndigits calculates number of decimal digits of the number. It is needed to print real triangle as all numbers have to have the same size (some will be padded by zeroes). Otherwise the trangle will not look like a triangle.

the rows * (rows + 1) /2 formula calculates the result of the 1+2+3+4+5... showing the largest number in the triangle

three row triangle will look:

1 
2 3 
4 5 6 

ten rows triangle:

01 
02 03 
04 05 06 
07 08 09 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 

and 20 rows:

001 
002 003 
004 005 006 
007 008 009 010 
011 012 013 014 015 
016 017 018 019 020 021 
022 023 024 025 026 027 028 
029 030 031 032 033 034 035 036 
037 038 039 040 041 042 043 044 045 
046 047 048 049 050 051 052 053 054 055 
056 057 058 059 060 061 062 063 064 065 066 
067 068 069 070 071 072 073 074 075 076 077 078 
079 080 081 082 083 084 085 086 087 088 089 090 091 
092 093 094 095 096 097 098 099 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 

you can try it yourself here: https://godbolt.org/z/7EWWso

You can use This Simple Code:

#include <stdio.h>

int main(){
    int count=0;
    for (int i = 01; i < 11; ++i) {

        for (int j = 01; j <=i; ++j) {
            count+=01;
            printf("%d ",count);
        }
        printf("\n");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM