You are here:

C/program in c

Advertisement


Question
Write a program in C using only loops to print
       1
      121
     12321
    1234321
   123454321

Answer
Hello Sanando

If you are having trouble with this, then try to solve a simpler problem. Once you have the simpler problem solved, then move on the the next level. For example, try to solve this problem:
12345
12345
12345
12345
12345

To solve this problem you need 2 loops, one nested in the other. The inner loop controls printing of each digit. The outer loop controls printing of the newlines. For each iteration of the outer loop, one line is printed. Here is the code

int outer
int inner
for (outer = 1; outer <= 5; ++outer)
{
  for (inner = 1; inner <= 5; ++inner)
  {
     printf("%d", inner);
  }
  printf ("\n");
}

Once you have that working, then change the inner loop to print
1
12
123
1234
12345
How will the inner limit change to get this effect?

Once you have that working then add code to count down to get the other half of each line
1
121
12321
1234321
123454321

Once you have that working then add code to print the spaces so that the output comes out in a triangle.
      1
     121
    12321
   1234321
  123454321

I've started the work for you now it is up to you to finish.

Kindest regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.