Print the triangle pattern outline using For Loop in C


This program is written in C programming language and is used to print a triangle pattern made up of asterisks (*) and spaces. The program starts by including the standard input/output library (stdio.h) and then defines the main function with the return type void. Within the main function, the program declares three variables "i", "j" and "n". The variable "n" is used to store the value entered by the user, which determines the size of the triangle pattern. The variables "i" and "j" are used as the counter variables for the nested for loops.

The program then prompts the user to enter a value using the "printf" function and stores the value in the variable "n" using the "scanf" function. Next, the program uses a nested for loop, with the outer loop controlled by the variable "i" and the inner loop controlled by the variable "j". The loops iterate from 1 to the value of "n" and print the pattern. Within the nested loops, the program uses an if statement to check the value of "i" and "j" and based on the values it will print either a "*" or a space.

The if statement check for the following conditions:

  • if the value of "j" is 1
  • if the value of "i" is equal to "n"
  • if the value of "j" is equal to "i" if any of the above conditions is met it will print "*" otherwise it will print " "

This will produce a triangle pattern with asterisks (*) at the beginning of each row and at the diagonal of the triangle shape and spaces in the middle. The program then ends with a return statement.

Source Code Example : 1

#include<stdio.h>
void main()
{
  int i,j,n;
  printf("\nEnter the value:");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    printf("\n");
    for(j=1;j<=n;j++)
    {
      if(j==1||i==n||j==i)
      {
        printf(" *");
      }
      else
        printf("  ");
    }
  }
  return 0;
}
        
To download raw file Click Here

Output

Enter the value:5
*
* *
*   *
*     *
* * * * *

Source Code Example : 2

#include<stdio.h>
int main()
{
  int i,j,n;
  printf("\nEnter the value:");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    printf("\n");
    for(j=1;j<=i;j++)
    {
      printf("  ");
    }
    for(j=n;j>=i;j--)
    {
      if(i==1||j==n||j==i)
      {
        printf(" *");
      }
      else
      {
        printf("  ");
      }
    }
  }
  return 0;
}
        
To download raw file Click Here

Output

Enter the value:5
* * * * *
 *      *
   *    *
     *  *
        *

Source Code Example : 3

#include<stdio.h>
int main()
{
  int i,j,n;
  printf("\nEnter the value:");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    printf("\n");
    for(j=1;j<=(n-i);j++)
    {
      printf("  ");
    }
    for(j=1;j<=i;j++)
    {
      if(j==1||i==n||j==i)
      {
        printf(" *");
      }
      else
      {
        printf("  ");
      }
    }
  }
  return 0;
}      
To download raw file Click Here

Output

Enter the value:5
        *
     *  *
   *    *
  *     *
* * * * *  

Source Code Example : 4

#include<stdio.h>
int main()
{
  int i,j,n;
  printf("\nEnter the Limit:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
     for(j=0;j<n-i;j++)
     {
        if(i==0||j==0||j==n-i-1)
        {
           printf(" *");
        }
        else
        {
           printf("  ");
        }
     }
     printf("\n");
  }
  return 0;
}        
To download raw file Click Here

Output

Enter the value:5
* * * * *
*     *
*   *
* *
*

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C