pyramid triangle

Hollow Pyramid Pattern in C using nested while

Hollow Pyramid Pattern in C using nested while

In this tutorial, we will discuss Hollow Pyramid Pattern using nested while loop in C programming language

In this program, we are going to learn about how to display  Hollow Pyramid Pattern using nested while loop in C programming  language

Here, we display two Hollow pyramid Pattern programs with coding using nested while loop and also program get input from the user using scanf() function in C  language

The user can provide numbers as they wish and get the  Hollow pyramid pattern according to their input.

Hollow Pyramid Pattern 1

Hollow Pyramid triangle star pattern

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows;
    printf("Enter the number of rows to Pyramid: ");
    scanf("%d",&rows);//get input from user for rows
    int i,j,k;
    i=1;
    while(i<=rows){//parent while loop(outer while)
        j=i;
        while(j<rows){
        printf(" ");//print initial space
        j++;
    }

    k=1;
while(k<2*i){
    if(i==rows || (k==1 || k==2*i-1))
        printf("*");//print star
    else
        printf(" ");//print space
    k++;
}
i++;
printf("\n");//Move to the next line for print
    }
getch();
return 0;
}

When the above code is executed, it produces the following results

Hollow Pyramid Pattern 2

Inverted Hollow Pyramid triangle star pattern

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows;
    printf("Enter the number of rows to Pyramid: ");
    scanf("%d",&rows);
    int i,j,k;
    i=rows;
    while(i>=1){//outer for loop
        j=rows;
        while(j>i){while loop print for space
        printf(" ");//print space
        j--;
    }

    k=1;
while(k<2*i){
    if(i==rows || (k==1 || k==2*i-1))
        printf("*");//print star
    else
        printf(" ");//print space
    k++;
}
i--;
printf("\n");//Move to the next line for print
    }
getch();
return 0;
}

When the above code is executed, it produces the following results

Hollow Pyramid Pattern 2

 

Suggested for you

Operator in C language

Data type in C language

While loop in C language

Nested while loop in C language

Hollow Pyramid Pattern in Cpp using nested while loop
Hollow Triangle Pattern using nested while loop in C
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago