C program to print natural numbers in right triangle pattern

C program to print natural numbers in right triangle pattern
  • Write a C program to print natural numbers right angled triangle.

For a right triangle of 4 rows. Program’s output should be:

Natural_Number_Triangle_Pattern

Required Knowledge

Algorithm to print natural number right triangle pattern using for loop

  • Take the number of rows(N) of right triangle as input from user using scanf function.
  • Number of integers in Kth row is always K. 1st row contains 1 integer, 2nd row contains 2 integers, 3rd row contains 3 integers and so on. In general, Kth row contains K integers.
  • We will use a integer counter to print consecutive natural numbers.
  • We will use two for loops to print right triangle of natural numbers.
    • Outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
    • For Kth row of right triangle pattern, inner loop will iterate K times. Each iteration of inner loop will increment the value of counter and print it on screen.

Here is the matrix representation of the natural number triangle pattern. The row numbers are represented by i whereas column numbers are represented by j.
Natural_Number_Triangle_Pattern

C program to print right triangle star pattern

C program to print natural numbers in right triangle pattern

#include<stdio.h>
 
int main() {
    int i, j, rows, count = 1;
 
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for (i = 0; i < rows; i++) {
        for (j = 0; j <= i; j++) {
            printf("%d ", count);
            count++;
        }
        printf("\n");
    }
    return(0);
}

Output

Enter the number of rows
4
1
2 3
4 5 6
7 8 9 10

C program to print triangle pattern having same number in a row

C program to print triangle pattern having same number in a row
  • Write a program in C to print right triangle having same number in a row.

For a right triangle of 6 rows, program’s output should be:
Triangle_Pattern_Same_Number_Row

Required Knowledge

Algorithm to print triangle pattern having same number in a row using for loop

  • Take the number of rows(N) of right triangle as input from user using scanf function.
  • Number of integers in Kth row is always K.
  • We will use two for loops to print right triangle of natural numbers.
    • Outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
    • For Kth row, Inner loop will iterate K times. Each iteration of inner loop will print row number on screen.

Here is the matrix representation of the above mentioned pattern. The row numbers are represented by i whereas column numbers are represented by j.
Triangle_Pattern_Same_Row

C program to print right triangle pattern having same number in a row

C program to print triangle pattern having same number in a row

#include<stdio.h>
 
int main() {
    int i, j, rows;
 
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for (i = 0; i < rows; i++) {
        for (j = 0; j <= i; j++) {
            printf("%d ", i+1);
        }
        printf("\n");
    }
    return(0);
}

Output

Enter the number of rows
6
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6

C program to print binary rectangular pattern where all boundary elements are 0 and all inner elements are 1

C program to print binary rectangle pattern
  • Write a C program to print binary rectangle pattern where all boundary elements are 0 and all inner elements are 1.

Binary_Number_Rectangle_Pattern

Required Knowledge

Algorithm to print rectangular pattern of binary digit 0 and 1

This program is similar to rectangle star pattern. At every position of rectangle we will check for boundary condition (first and last row and column of rectangle). If true, then we print 0 othewise 1.
Here is the matrix representation of the above mentioned binary pattern. The row numbers are represented by i whereas column numbers are represented by j.
Binary_Rectangle_Pattern

C program to print binary rectangle pattern

C program to print binary rectangle pattern

#include<stdio.h>
 
int main(){
    int rows, cols , i, j;
     
    printf("Enter rows and columns of rectangle\n");
    scanf("%d %d", &rows, &cols);
     
    /* Row iterator for loop */
    for(i = 0; i < rows; i++){
     /* Column iterator for loop */
        for(j = 0; j < cols; j++){
            /* Check if currely position is a boundary position */
            if(i==0 || i==rows-1 || j==0 || j==cols-1)
                printf("0");
            else
                printf("1");
        }
        printf("\n");
    }
    return 0;
}

Output

Enter rows and columns of rectangle
4 7
0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 1 1 1 1 0 
0 0 0 0 0 0 0

C program to print right triangle star pattern

  • Write a program in C to print right triangle star (*) pattern of n rows using for loop.
  • Write a C program to print a right angles triangle pattern of star (*) character using loops.

For a right triangle star pattern of 7 rows. Program’s output should be:

Right_Triangle_Star_Pattern_Program

Required Knowledge

Algorithm to print right triangle star pattern using loop

  • Take the number of rows(N) of right triangle as input from user using scanf function.
  • Number of stars in Kth row is always K. 1st row contains 1 star, 2nd row contains 2 stars, 3rd row contains 3 stars. In general, Kth row contains K stars.
  • We will use two for loops to print right triangle star pattern.
  • For a right triangle star pattern of N rows, outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
  • For Kth row of right triangle pattern, inner loop will iterate K times. Each iteration of inner loop will print one star (*).

Here is the matrix representation of the triangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Triangle_Star_Pattern_Matrix

C program to print right triangle star pattern

C program to print right triangle star pattern

#include<stdio.h>
 
int main() {
    int i,j,rows;
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for(i = 0; i < rows; i++) {
        /* Prints one row of triangle */
        for(j = 0; j <= i; ++j) {
           printf("* ");
        }
        /* move to next row */
        printf("\n");
    }
    return 0;
}

Output

Enter the number of rows
6
*
* *
* * *
* * * *
* * * * *
* * * * * *

C program to print diamond star pattern

  • Write a program in C to print diamond star pattern using for loop.
  • How to print a diamond shape pattern of using loops in C.

Diamond star pattern program’s output should be:

Diamond_Star_Pattern

Required Knowledge

Algorithm to print diamond star pattern using loop
Diamond star pattern is a combination of pyramid star pattern and inverse pyramid star pattern. This program is combination of both, it first prints a pyramid followed by a reverse pyramid star pattern.

C program to print diamond star pattern

C program to print diamond star pattern

#include<stdio.h>
 
int main() {
    int i, space, rows=7, star=0;
     
    /* Printing upper triangle */
    for(i = 1; i <= rows; i++) {
        /* Printing spaces */
        for(space = 1; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        while(star != (2*i - 1)) {
            printf("*");
            star++;;
        }
        star=0;
        /* move to next row */
        printf("\n");
    }
    rows--;
    /* Printing lower triangle */
    for(i = rows;i >= 1; i--) {
        /* Printing spaces */
        for(space = 0; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        star = 0;
        while(star != (2*i - 1)) {
            printf("*");
            star++;
        }
        printf("\n");
    }
 
    return 0;
}

Output

     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

C program to print hollow diamond star pattern

  • Write a C program to print hollow diamond star pattern.

Hollow Diamond star pattern program’s output should be:

Hollow_Diamond_Star_Pattern

Required Knowledge

Algorithm to print hollow diamond star pattern using for loop

This program is similar to diamond star pattern. The only difference is, here we will only print first and last star character of any row and we will replace all internal star characters by space character.

C program to print hollow or empty diamond star pattern

C program to print hollow diamond star pattern

#include<stdio.h>
 
int main() {
    int i, space, rows=7, star=0;
     
    /* Printing upper triangle */
    for(i = 1; i <= rows; i++) {
        /* Printing spaces */
        for(space = 1; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        while(star != (2*i - 1)) {
         if(star == 0 or star==2*i-2)
                printf("*");
            else
                printf(" ");
            star++;
        }
        star=0;
        /* move to next row */
        printf("\n");
    }
    rows--;
    /* Printing lower triangle */
    for(i = rows;i >= 1; i--) {
        /* Printing spaces */
        for(space = 0; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        star = 0;
        while(star != (2*i - 1)) {
         if(star == 0 or star==2*i-2)
                printf("*");
            else
                printf(" ");
            star++;
        }
        printf("\n");
    }
 
    return 0;
}

Output

     *
    * *
   *   *
  *     *
 *       *
*         *
 *       *
  *     *
   *   *
    * *
     *

C program to print Hut star pattern

  • Write a C program to print a Hut star pattern.

Hut star pattern program’s output should be:

Hut_Star_Pattern

Required Knowledge

Algorithm to print hut star pattern

Printing a hut star pattern is a two step process.

  • Step 1: Print pyramid star pattern for top 5 rows. Check this c program to print pyramid star pattern.
  • Step 2: Print the bottom half of the hut. Each row of the bottom half of hut contains 3 stars then three space characters followed by 3 stars.

Here is the matrix representation of the Hut star pattern. The row numbers are represented by i whereas column numbers are represented by j.

Hut_Star_Pattern

C program to print Hut star pattern on screen

C program to print Hut star pattern

#include<stdio.h>
 
int main() {
    int i, j, space, rows = 8, star = 0;
 
    /* Printing upper triangle */
    for (i = 0; i < rows; i++) {
        if (i < 5) {
            /* Printing upper triangle */
            for (space = 1; space < 5 - i; space++) {
                printf(" ");
            }
            /* Printing stars */
            while (star != (2 * i + 1)) {
                printf("*");
                star++;;
            }
            star = 0;
            /* move to next row */
            printf("\n");
        } else {
            /* Printing bottom walls of huts */
            for (j = 0; j < 9; j++) {
                if ((int) (j / 3) == 1)
                    printf(" ");
                else
                    printf("*");
            }
            printf("\n");
        }
    }
    return 0;
}

Output

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

C program to print heart star pattern

  • Write a C program to print a heart shape star pattern.
  • How to draw a Heart on screen using star characters in C.

Heart star pattern program’s output should be:

Heart_Star_Pattern

Required Knowledge

C program to print Heart star pattern on screen

C program to print heart star pattern

#include <stdio.h>  
   
int main() {  
    int i,j, rows;
     
    printf("Enter the number of rows\n");
    scanf("%d", &rows);  
    /* printing top semi circular shapes of heart */
    for(i = rows/2; i <= rows; i+=2){ 
     /* Printing Spaces */
        for(j = 1; j < rows-i; j+=2) {  
            printf(" ");  
        }
        /* printing stars for left semi circle */
        for(j = 1; j <= i; j++){  
            printf("*");  
        }  
        /* Printing Spaces */
        for(j = 1; j <= rows-i; j++){  
            printf(" ");  
        }  
        /* printing stars for right semi circle */
        for(j = 1; j <= i; j++){  
            printf("*");  
        }  
        /* move to next row */
        printf("\n");  
    }  
     
    /* printing inverted start pyramid */
    for(i = rows; i >= 1; i--){  
        for(j = i; j < rows; j++){  
            printf(" ");  
        }  
        for(j = 1; j <= (i*2)-1; j++){  
            printf("*");  
        }  
        /* move to next row */
        printf("\n");  
    }  
   
    return 0;  
}

Output

Enter the number of rows
7
 **   **
**** ****
*********
 *******
  *****
   ***
    *

C program to print hollow pyramid star pattern

  • Write a C program to print hollow pyramid star pattern.

Hollow pyramid star pattern program’s output should be:

Hollow_Pyramid_Pattern

Required Knowledge

Algorithm to print hollow pyramid star pattern using for loop

This program is similar to pyramid star pattern. The only difference is, from first to second last row we will only print first and last star character of any row and we will replace all internal star characters by space character. Then we will print 2*N-1 (N = number of rows in pattern) star characters in last row.

Here is the matrix representation of the hollow pyramid star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Hollow_Pyramid_Star_Pattern

C program to print hollow pyramid star pattern

C program to print hollow pyramid star pattern

#include<stdio.h>
 
int main() {
    int i, space, rows, star=0;
    printf("Enter the number of rows\n");
    scanf("%d",&rows);
 
    /* printing one row in every iteration */
    for(i = 0; i < rows-1; i++) {
        /* Printing spaces */
        for(space = 1; space < rows-i; space++) {
            printf(" ");
        }
        /* Printing stars */
        for (star = 0; star <= 2*i; star++) {
            if(star==0 || star==2*i)
                printf("*");
            else
                printf(" ");
        }
        /* move to next row */
        printf("\n");
    }
    /* print last row */
    for(i=0; i<2*rows-1; i++){
        printf("*");
    }
    return 0;
}

Output

Enter the number of rows
6
     *
    * *
   *   *
  *     *
 *       *
***********

C program to print square star pattern

  • Write a C program to print square star pattern of n rows using for loop.

For a square star pattern of side 5 stars. Program’s output should be:

Square_Star_Pattern_program

Required Knowledge

Algorithm to print square star pattern using loop

  • Take the number of stars in each side of square as input from user using scanf function. Let it be N.
  • We will use two for loops to print square star pattern.
  • In one iteration, outer for loop will print one row of pattern.
  • In one iteration, inner for loop will print one star (*) characters in a row.

Here is the matrix representation of the square star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Square_Star_Pattern_Matrix

C program to print square star pattern

C program to print square star pattern

#include<stdio.h>
 
int main(){
    int side, i, j;
     
    printf("Enter side of square\n");
    scanf("%d", &side);
     
    /* Row iterator for loop */
    for(i = 0; i < side; i++){
     /* Column iterator for loop */
        for(j = 0; j < side; j++){
           printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output

Enter side of square
5
*****
*****
*****
*****
*****