Friday 20 September 2013

Friday 20 September 2013

Program to print triangle in C


C program to print Triangle in C:

#include<stdio.h>
main()
{
    int j,i,k,l;
    char c;
    do
    {
        printf("Enter the number of stars in the base..\n");
        scanf("%d",&k);
        for (i=1;i<=k;i++)  
        {  
            for(l=0;l<(k-i);l++)  
                printf(" ");  
            for (j=0;j<i;j++)  
                printf ("1 ");  
            printf("\n");  
        }  
        printf("\n Enter y to try more...press any other key exit...\t");
    c=getchar();
    c=getchar();
    }
    while(c=='y');
}

------------------------------------------------------------------------------------------------

without using do while loop:

#include<stdio.h>  
main()
{
    int j,i,k,l;
    k=6;
    
    
        for (i=1;i<=k;i++)  
        {  
            for(l=0;l<(k-i);l++)  
                printf(" ");  
            for (j=0;j<i;j++)  
                printf ("* ");  
            printf("\n");  
        }  
}
-------------------------------------------------------------------------------------------------------

Executing the program....
$demo
Enter the number of stars in the base.. 6
     1 
    1 1 
   1 1 1 
  1 1 1 1 
 1 1 1 1 1 
1 1 1 1 1 1 

 Enter y to try more...press any other key exit...

No comments:

Post a Comment