Thread: triangle number

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    triangle number

    Hey guys,

    I am Kyle, kinda new here, I got problem with writing a program that will print triangle in screen;
    0
    010
    01210
    0123210
    012343210
    01234543210
    0123456543210

    here is what i got so far, i dont know how to write the number in each line

    Code:
    #include <stdio.h>
    
    int main(void) {
    	int num;
    	printf ("Welcome to the Parymid program \n");
    	printf ("Enter your number (1-9): ");
    	scanf ("%d", &num);
    	int i  ;
    	int j ;
    	
    	for ( i = 0 ; i <= num ; i = i+1 ) {
    		for ( j = 0 ; j <= i ; j = j+1 ) {
    			printf ("%d ", j) ;
    	        }
    	printf ("\n");
    	}
    return 0;
    }
    Any ideas ??? please let me know

    Thanks
    Kyle Huynh

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    by the way, that is not supposed to be right triangle

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Start with asking yourself this question:

    If a users enters a number, what is the maximum width of the longest line? When you get the answer for that, come up with (in math terms) a function that will calculate the maximum width.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Note,
    Code:
    i = i+1
    Can be written as
    Code:
    i++;
    You've got the triangle there, just the lack of spaces have messed it up. A simple rule is, the next line should have 1 more space on the left than the previous. OR you could say, it has one less each time from the maximum line (see Dino's post).

    That means the 2nd last line (at the bottom) has 1 more space on the left than the maximum line (the last line). The 3rd last line will have 2 more spaces than the last line, the 4th last line will have 3 more spaces than the last line. Eg:

    Code:
    0 
    010
    01210
    0123210            ...
    012343210          Should have N+2 spaces on the left
    01234543210        Should have N+1 spaces on the left
    0123456543210      Length of 'N' spaces on the left (none in this case)
    Which ever way is easier for you to visualize, is the one you should use.
    Last edited by zacs7; 09-10-2008 at 07:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM