A pattern program in C language is an easy and fun way to practice loops and logical thinking. Pattern programs mean printing different shapes like triangles, pyramids, diamonds, stars, numbers, and alphabets using C code.
Below,
you'll find easy ways to print different patterns clearly and step-by-step,
making it simple for beginners to master pattern printing in C programming.
Hinglish
Coders'
comments
Practice
patterns neat and clear, A coding career you'll build right here!
Option 1: C
Pattern Program for Right-Angled Triangle
Code:
#include
<stdio.h>
int main()
{
// i -> rows loop, j -> columns loop,
// rows -> total rows
int i, j, rows = 5;
// Outer loop controls number of rows
for(i = 1; i <= rows; i++) {
// Inner loop prints stars in each row
for(j = 1; j <= i; j++) {
// Print star
printf("*");
}
// Move to next line after each row
printf("\n");
}
return 0;
}
Run Code
Output:
*
**
***
****
*****
Explanation:
This
triangle pattern program in C language prints a simple right-angled triangle
using stars. It uses nested loops, where the outer loop controls rows and the
inner loop prints stars. Such basic C programs on patterns help beginners
clearly understand loops.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 2: C
Pattern Program for Inverted Right-Angled Triangle
Code:
#include
<stdio.h>
int main()
{
int i, j, rows = 5;
// Outer loop runs in reverse (top to
bottom)
for(i = rows; i >= 1; i--) {
// Inner loop prints decreasing stars
for(j = 1; j <= i; j++) {
printf("*");
}
// New line after each row
printf("\n");
}
return 0;
}
Run Code
Output:
*****
****
***
**
*
Explanation:
This
triangle pattern program in C language prints an inverted right-angled
triangle. The outer loop runs in reverse (decreasing row size), and the inner
loop prints stars for each row. Understanding inverted triangles is helpful in
creating more complicated patterns. Such simple triangle pattern code in C is
asked in beginner-level coding exams.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 3:
Pattern Program in C For Equilateral Triangle
Code:
#include
<stdio.h>
int main()
{
int i, space, j, rows = 5;
// Loop for each row
for(i = 1; i <= rows; i++) {
// Print spaces for alignment
for(space = 1; space <= rows - i;
space++)
printf(" ");
// Print stars (odd count: 2*i - 1)
for(j = 1; j <= (2*i - 1); j++)
printf("*");
printf("\n");
}
return 0;
}
Run Code
Output:
*
***
*****
*******
*********
Explanation:
This C
program to print a pattern creates an equilateral or full pyramid triangle. It
uses nested loops, first printing spaces and then stars. It's a popular
triangle pattern in C program for understanding alignment and symmetry. Such C
programs on patterns are frequently asked in interviews and tests to assess
loop logic.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 4:
Pattern Program in C for Pascal's Triangle
Code:
#include
<stdio.h>
// Function
to calculate factorial using recursion
int
fact(int n) {
if(n == 0)
// Base case
return 1;
else
// Recursive call
return n * fact(n - 1);
}
int main()
{
int i, j, rows = 5;
// Loop through rows
for(i = 0; i < rows; i++) {
// Print spaces for formatting
for(j = 0; j <= rows - i - 1; j++)
printf(" ");
// Print Pascal values using formula
nCr
for(j = 0; j <= i; j++)
printf("%d ", fact(i) /
(fact(j) * fact(i - j)));
printf("\n");
}
return 0;
}
Run Code
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Explanation:
This
classic triangle pattern program in C language prints Pascal's triangle using
factorial logic. Each number is calculated based on combinations (binomial
coefficients). Practicing this triangle pattern code in C helps strengthen
understanding of mathematical logic and recursion in programming. Pascal's
triangle pattern is common in programming and mathematics.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 5: C
Pattern Program for Hollow Triangle
Code:
#include
<stdio.h>
int main()
{
int i, j, rows = 5;
// Loop for rows
for(i = 1; i <= rows; i++) {
// Print leading spaces
for(j = i; j < rows; j++)
printf(" ");
// Print stars and spaces
for(j = 1; j <= (2*i - 1); j++) {
// Print star only at borders or last row
if(i == rows || j == 1 || j == (2*i
- 1))
printf("*");
else
// Inner hollow space
printf(" ");
}
printf("\n");
}
return 0;
}
Run Code
Output:
*
* *
* *
* *
*********
Explanation:
This C
program to print a pattern shows how to create a hollow equilateral triangle.
It prints stars only at the triangle's edges using conditional statements
within loops. Such a triangle pattern in C program improves your understanding
of nested loops and conditionals.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 6:
Pattern in C Programming for Number Triangle
Code:
#include
<stdio.h>
int main()
{
int i, j, rows = 5;
// Loop through rows
for(i = 1; i <= rows; i++) {
// Print numbers from 1 to i
for(j = 1; j <= i; j++) {
printf("%d ", j);
}
// Next line
printf("\n");
}
return 0;
}
Run Code
Output:
1
1 2
1 2 3
1 2 3 4 1 2
3 4 5
Explanation:
This easy
triangle pattern program in C language demonstrates printing numbers sequentially
in each row. Such number patterns strengthen your looping logic and numeric
control. This kind of triangle pattern in C programming is common in academic
exams and is an excellent practice for beginners learning nested loops and
number manipulation.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 7: C
Pattern Program for Alphabet Triangle
Code:
#include
<stdio.h>
int main()
{
int i, j, rows = 5;
char ch;
// Loop through rows
for(i = 1; i <= rows; i++) {
// Reset character for each row
ch = 'A';
// Print alphabets
for(j = 1; j <= i; j++) {
// Print and increment character
printf("%c ", ch++);
}
printf("\n");
}
return 0;
}
Run Code
Output:
A
A B
A B C
A B C D
A B C D E
Explanation:
This
interesting triangle pattern in C programming shows alphabet patterns. The
program uses character increments (ASCII values) to print alphabets in
sequence. Practicing alphabet patterns helps clearly understand character
manipulation in C. Such triangle pattern code in C is useful for beginners to
master character handling and nested loops effectively.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 8: C
Pattern Program for Inverted Full Pyramid
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j, space;
// Loop for rows (reverse)
for (i = rows; i >= 1; i--) {
// Print spaces
for (space = 1; space <= rows - i;
space++)
printf(" ");
// Print stars
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
Run Code
Output:
*********
*******
*****
***
*
Explanation:
This
pyramid pattern in C programming prints an inverted pyramid of stars. It
reverses the logic of the full pyramid, decreasing the number of stars per row
while increasing initial spaces. This C program to print a pattern helps in
understanding loop logic clearly, often tested in beginner programming assignments.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 9:
Pattern Program in C for Half Pyramid
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j;
// Outer loop runs from 1 to number of rows
for (i = 1; i <= rows; i++) {
// Inner loop prints stars in each row
// Number of stars increases with each
row
for (j = 1; j <= i; j++)
// Print star
printf("*");
// Move to next line after each row
printf("\n");
}
return 0;
}
Run Code
Output:
*
**
***
****
*****
Explanation:
The half
pyramid pattern program in C language is one of the simplest pattern programs.
It prints stars increasing with each row using nested loops. Such simple
pyramid pattern in C programming forms the basic understanding required for
advanced patterns. Beginners often practice this pattern first when learning
nested loops.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 10:
C Pattern Program for Inverted Half Pyramid
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j;
// Outer loop runs from rows to 1
(decreasing order)
for (i = rows; i >= 1; i--) {
// Inner loop prints stars for each row
// Number of stars decreases with each
row
for (j = 1; j <= i; j++)
printf("*"); // Print star
printf("\n"); // Move to next line after each row
}
return 0;
// Indicates successful execution
}
Run Code
Output:
*****
****
***
**
*
Explanation:
This simple
inverted half pyramid pattern in C programming decreases the number of stars in
each subsequent row. It's a beginner-friendly pyramid pattern code in C
frequently asked in interviews and tests. Practicing this pattern strengthens
your loop-handling logic and helps in understanding decremental loops.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 11:
C Pattern Program for Hollow Pyramid Pattern
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j, space;
// Outer loop runs from 1 to total rows
for (i = 1; i <= rows; i++) {
// Print spaces to align the pyramid
shape
for (space = 1; space <= rows - i;
space++)
printf(" ");
// Print stars and inner spaces
for (j = 1; j <= 2 * i - 1; j++) {
// Print star at boundaries and
last row
if (j == 1 || j == 2 * i - 1 || i
== rows)
printf("*");
else
// Print space for hollow part
printf(" ");
}
printf("\n");
}
return 0;
}
Run Code
Output:
*
* *
* *
* *
*********
Explanation:
This hollow
pyramid pattern program in C language prints stars only at pyramid borders,
leaving inside empty. It uses nested loops and conditional statements for clear
border definition. Such pyramid pattern code in C improves understanding of
nested loops and logical conditions, often used in coding exams and interviews.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 12:
C Pattern Program for Alphabet Pyramid
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j, space;
char ch = 'A';
// Outer loop runs from 1 to total rows
for (i = 1; i <= rows; i++) {
// Print spaces for pyramid alignment
for (space = 1; space <= rows - i;
space++)
printf(" ");
// Reset character to 'A' at the start
of each row
ch = 'A';
// Print alphabets in sequence
for (j = 1; j <= 2 * i - 1; j++)
// Print character and increment it
printf("%c", ch++);
printf("\n");
}
return 0;
}
Run Code
Output:
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
Explanation:
This
alphabet-based full pyramid pattern program in C language demonstrates
incrementing alphabetically across each row. It clearly shows character
handling using ASCII values and nested loops. Such alphabet pyramid patterns
are valuable to beginners for improving their character and loop management
skills, useful in many coding tests.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 13:
Solid Diamond Pattern Printing in C
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j, space;
// Upper half of diamond (pyramid shape)
for(i = 1; i <= rows; i++) {
// Print spaces for alignment
for(space = 1; space <= rows - i;
space++)
printf(" ");
// Print stars (odd count: 2*i - 1)
for(j = 1; j <= 2*i - 1; j++)
printf("*");
printf("\n");
}
// Lower half of diamond (inverted pyramid)
for(i = rows - 1; i >= 1; i--) {
// Print spaces for alignment
for(space = 1; space <= rows - i;
space++)
printf(" ");
// Print stars (decreasing count)
for(j = 1; j <= 2*i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
Run Code
Output:
*
***
*****
*******
*********
*******
*****
***
*
Explanation:
This
diamond pattern program in C language consists of two pyramids: one upright and
one inverted. It clearly shows symmetry and spacing logic using nested loops.
This pattern printing in C programming example is useful in strengthening loop
logic and is often asked in programming exams to test logical skills.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 14:
Hollow Diamond Pattern Printing in C
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j, space;
// Upper half of hollow diamond
for(i = 1; i <= rows; i++) {
// Print spaces for alignment
for(space = 1; space <= rows - i;
space++)
printf(" ");
// Print stars at boundaries, spaces
inside
for(j = 1; j <= 2*i - 1; j++)
if(j == 1 || j == 2*i - 1)
// Print star at edges
printf("*");
else
// Print space for hollow part
printf(" ");
printf("\n");
}
// Lower half of hollow diamond
for(i = rows - 1; i >= 1; i--) {
// Print spaces for alignment
for(space = 1; space <= rows - i;
space++)
printf(" ");
// Print stars at boundaries, spaces
inside
for(j = 1; j <= 2*i - 1; j++)
if(j == 1 || j == 2*i - 1)
printf("*");
else
printf(" ");
printf("\n");
}
return 0;
}
Run Code
Output:
*
* *
* *
* *
* *
* *
* *
* *
*
Explanation:
This hollow
diamond pattern program in C language prints stars only at the edges, creating
a hollow diamond shape. It requires clear conditional logic inside loops.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 15:
Rhombus Pattern Printing in C Programming
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j, space;
// Outer loop runs from 1 to total rows
for(i = 1; i <= rows; i++) {
// Print spaces to shift the pattern
right
for(space = 1; space <= rows - i; space++)
printf(" ");
// Print fixed number of stars in each
row
for(j = 1; j <= rows; j++)
printf("*");
printf("\n");
}
return 0;
}
Run Code
Output:
*****
*****
*****
*****
*****
Explanation:
The rhombus
pattern program in C language is a shifted rectangle that forms a rhombus. The
program uses loops clearly to manage initial spaces before printing stars.
Time
Complexity Analysis
Time Complexity
: O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 16:
Hollow Rhombus Pattern Program in C
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j, space;
// Outer loop runs through each row
for(i = 1; i <= rows; i++) {
// Print spaces to shift the rhombus
shape
for(space = 1; space <= rows - i;
space++)
printf(" ");
// Print stars at borders, spaces
inside
for(j = 1; j <= rows; j++) {
// Print star at first row, last
row, first column,
// or last column
if(i == 1 || i == rows || j == 1 ||
j == rows)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Run Code
Output:
*****
* *
* *
* *
*****
Explanation:
This hollow
rhombus pattern program in C language prints stars only on the boundary,
creating a clear hollow effect. It demonstrates how nested loops and
conditionals manage shapes precisely.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space Complexity
Analysis
Space
Complexity : O(1)
Option 17:
C Program to Print Hourglass Pattern
Code:
#include
<stdio.h>
int main()
{
int rows = 5, i, j, space;
// Upper half of hourglass (inverted
pyramid)
for(i = rows; i >= 1; i--) {
// Print spaces for alignment
for(space = 0; space < rows - i;
space++)
printf(" ");
// Print stars (decreasing count)
for(j = 1; j <= (2*i - 1); j++)
printf("*");
printf("\n");
}
// Lower half of hourglass (normal pyramid)
for(i = 2; i <= rows; i++) {
// Print spaces for alignment
for(space = 0; space < rows - i;
space++)
printf(" ");
// Print stars (increasing count)
for(j = 1; j <= (2*i - 1); j++)
printf("*");
printf("\n");
}
return 0;
}
Run Code
Output:
*********
*******
*****
***
*
***
*****
*******
*********
Explanation:
The
hourglass pattern program in C language combines inverted and upright pyramids.
It illustrates clear symmetry using nested loops and spacing logic.
Time
Complexity Analysis
Time
Complexity : O(n²)
Space
Complexity Analysis
Space
Complexity : O(1)
No comments:
Post a Comment