Pyramid of numbers and time complexity

i must print this pyramid with n=6 in C
66
5665
456654
34566543
2345665432
123456654321

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

#include <stdio.h>
int main() {
   int i, space, rows, k = 0, count = 0, count1 = 0,x=0;
   printf("Enter the number of rows: ");
   scanf("%d", &rows);
   for (i = rows; i >= 1; --i) {
   	  if(x<(rows*2)) x+=2;
      for (space = 1; space <= i; ++space) {
         printf("  ");
         ++count;
      }
      while (k !=x) {
         if (count <= rows) {
            printf("%d ", i+k);
            ++count;
         } else {
            ++count1;
            printf("%d ", (i + k - 2 * count1));
         }
         ++k;
      }
      count1 = count = k = 0;
      printf("\n");
   }
   return 0;
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

What errors are you seeing? How does your output compare to the expected output?

the code is giving this output:
6 5
5 6 5 4
4 5 6 5 4 3
3 4 5 6 5 4 3 2
2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1 0

Minor quibble - the compiler turns your code into machine code that your computer runs. The compiler is not making the output of the program. Your code makes that output.

It looks like an off by one error. Where are you calculating the decreasing values for the right side of the row?

Some comments and more descriptive variable names would help a lot.

while (k !=x) {
         if (count <= rows) {//for the left side
            printf("%d ", i+k);
            ++count;
         } else {// for the right side
            ++count1;
            printf("%d ", (i + k - 2 * count1));
         }
         ++k;
      }

i can’t find the formula for calculating the right side

The simple fix would be to add 1 to this calculation here.

It worked. Thank you for helping me!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.