Thread: Empty tringle

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    73

    Empty tringle

    Hello ,

    I'm trying to coding for making an empty tringle with all side with stars. Anyone help me to correct it ?




    Code:
     #include<stdio.h>
    
    void main()
    
    {
    	int n,i,l;
    	scanf("%d",&n);
    
    	for(i=1;i<=n;i++)
    
    	{
    	 for(l=1;l<=n-i;l++)
    
    	 { printf(" ");}
    
    	 if(i<=n)
    
    	 { printf("*");}
    
    	 if(1<i<n)
    
    	 {
    
    	 for(l=1;l<=2*i-3;l++)
    
    	 {printf(" ");}}
    
    	 if(1<i<n)
    
    		 { printf("*");}
    
    	 if(i==n)
    
    	 {
    	  for(l=1;l<=2*i-2;l++)
    		  printf("*");
    	 }
    
    	 printf("\n");
    
    
    	}
    
    }


  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    if(1<i<n)
    C doesn't handle comparisons with that format, use:
    Code:
    if(1<i && i<n)
    instead.

    Once you have the left half of the triangle being printed correctly, the right half will be simple to code up. I'd concentrate on the left half.

    Since C arrays always start at zero, the for loops tend to be done in that same way. Not i=1;i<=n;i++, but i=0;i<n;i++. It eliminates the possibility of two errors immediately and, puts your code in a format much more familiar to other C programmers.
    Last edited by Adak; 12-11-2010 at 07:25 PM.

  3. #3
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    If you're going to use printf, why not use printf formatted output?
    Code:
    #include<stdio.h>
    int main (void) {
        int i = 10;
        int n = i;
        printf ("%*s\n",i+1,"*");
        while (--i) printf ("%*s%*s\n",i+1,"*",n*2-i*2,"*");
        while (n--) printf ("**");
        printf ("*\n");
        return 0;
    }
    Should also validate input so the user doesn't enter 0.
    Last edited by hellork; 12-11-2010 at 08:01 PM. Reason: this one's more readable :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 06:02 AM
  2. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  3. Finding whether UCHAR is empty?
    By Witchfinder in forum C Programming
    Replies: 4
    Last Post: 04-25-2009, 10:19 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. skipping empty files using ifstream
    By bradleym83 in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2005, 07:15 AM