floyed's triangle

This is a discussion on floyed's triangle within the C Programming forums, part of the General Programming Boards category; I'm having a problem writing a code to build a triangle that will expand depending on the user. How do ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    floyed's triangle

    I'm having a problem writing a code to build a triangle that will expand depending on the user.
    How do u tell C to go to the next line with each number?
    You have to enter a number and the triangle will grow...example

    entering 2 will result in
    1
    23

    I know you have to use a for loop such as :

    for (k=1;k<100;k++)
    {
    printf("%d")
    }

    can anyone help

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Something like this?:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void show ( int *val, int n )
    {
      while ( n-- > 0 )
        printf ( "%02d ", (*val)++ );
      printf ( "\n" );
    }
    
    void triangle ( int n )
    {
      int i, inc = 1;
    
      for ( i = 1; i <= n; i++ )
        show ( &inc, i );
    }
    
    int main ( void )
    {
      int i;
    
      fputs ( "Enter a number: ", stdout );
      if ( scanf ( "%d", &i ) != 1 ) {
        fputs ( "Error reading input\n", stderr );
        exit ( EXIT_FAILURE );
      }
    
      triangle ( i );
    
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 01:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  3. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 05:40 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. Determining a Triangle using get and pointer
    By naynay in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 05:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21