Thread: coding problem

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    1
    Rep Power
    0

    Exclamation coding problem


    I am trying to develop a program to display like this
    123454321
    1234 4321
    123 321
    12 21
    1 1


    I worked out as

    #include <stdio.h>

    int main()
    {

    int i,j,k,l=5;

    for(k=1;k<=5;k++)
    {
    for(j=1;j<=l;j++)
    printf("%d",j);
    l--;

    for(i=l;i>=1;i--)
    printf("%d",i);

    putchar('\n');

    }

    getchar();
    return 0;
    }


    but not getting desired results
    can anybody help me?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    This looks distinctly like homework, since I can't imaginea situation outside of a textbook where you'd want to do this. It's pretty simple though, and you'll probably only need two variables to pull it off.

    First, you need an outer loop to count from your highest number down to 0. I actually defined a third variable called trigger to hold this highest number, but there's no reason to.

    Now you need two loops inside of the outer loop.

    The first one counts from 1 to the trigger value. If the inner loop value is less than or equal to the outer loop value, print the value of the inner loop, otherwise a space.

    The second inner loop executes after the first inner loop. It counts from 1 less than the trigger value to 0. It uses the same printing conditions.

    I won't give you the specific code, but I have tried this out and it works nicely.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo