C Program to Print Half Pyramid Pattern of Numbers | Stars | Alphabets
ADVERTISEMENTS
C program to print half pyramid pattern of numbers, stars, and alphabets.
In this article, you will learn how to print the half-pyramid patterns of the numbers and stars in the c programming language.
Examples
Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
*
* *
* * *
* * * *
* * * * *
You should have knowledge of the following topics in c programming to understand these programs:
- C
main()
function - C
for
loop statement - C
if
condition statement - C
increment
operator - C
printf()
function
In this article, we solve this problem in three methods:
C Program to Print Half Pyramid Pattern of Numbers
// C Program to Print Half Pyramid Pattern of Numbers
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
if (rows > 0) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
C Program to Print Half Pyramid Pattern of Stars
// C Program to Print Half Pyramid Pattern of Stars
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
if (rows > 0) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
C Program to Print Half Pyramid Pattern of Alphabets
// C Program to Print Half Pyramid Pattern of Alphabets
#include <stdio.h>
int main() {
int rows;
char alphabet = 'A';
printf("Enter the number of rows: ");
scanf("%d", &rows);
if (rows > 0) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ", alphabet);
}
alphabet++;
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows: 5
A
B B
C C C
D D D D
E E E E E
Explanation
In the given program, we have taken input 5
from the user and passed this input for loop iteration.
Then It follows the perspective logic to print the pattern.
Also, visit these links
Half Pyramid in C Programming of Stars
Half Pyramid Pattern in C++ language of Stars