PPS Lab · Experiment 41
Number and star pyramid patterns
CMiscellaneousJNTUH R22 · CS108ES
Aim
To write a C program that prints five patterns of numbers and stars: 1 / 1 2 / 1 2 3, a star triangle, 1 / 2 3 / 4 5 6, 1 / 2 2 / 3 3 3, and a star pattern that grows and then shrinks. The number of rows for each pattern is read from the user.
Algorithm
- For each pattern, read the number of rows. If it is not between 1 and 20, print an error and stop.
- Pattern 1: for row i from 1 to rows, print the numbers 1 to i.
- Pattern 2: for row i from 1 to rows, print i stars.
- Pattern 3: keep a counter starting at 1. For row i from 1 to rows, print the counter i times, adding 1 after each print.
- Pattern 4: for row i from 1 to rows, print the number i, i times.
- Pattern 5: for i from 1 up to the widest row, print i stars. Then for i from (widest − 1) down to 1, print i stars.
- Print a newline at the end of every row.
Program
#include <stdio.h>
#include <stdlib.h>
/* Shows the prompt and reads a row count from 1 to 20. Stops the program on bad input. */
int read_rows(const char prompt[]) {
int rows;
printf("%s", prompt);
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 20) {
printf("Rows must be a number from 1 to 20\n");
exit(1);
}
return rows;
}
/* 1 / 1 2 / 1 2 3 */
void pattern1(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
}
/* * / * * / * * * */
void pattern2(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
/* 1 / 2 3 / 4 5 6 (numbers keep counting up) */
void pattern3(int rows) {
int num = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
}
/* 1 / 2 2 / 3 3 3 / 4 4 4 4 (row number repeated) */
void pattern4(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}
}
/* * / * * / * * * / * * / * (grows to the widest row, then shrinks) */
void pattern5(int widest) {
for (int i = 1; i <= widest; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
for (int i = widest - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
int main(void) {
int rows;
rows = read_rows("Enter the number of rows for pattern 1: ");
pattern1(rows);
rows = read_rows("\nEnter the number of rows for pattern 2: ");
pattern2(rows);
rows = read_rows("\nEnter the number of rows for pattern 3: ");
pattern3(rows);
rows = read_rows("\nEnter the number of rows for pattern 4: ");
pattern4(rows);
rows = read_rows("\nEnter the widest row for pattern 5: ");
pattern5(rows);
return 0;
}Output
Enter the number of rows for pattern 1: 3
1
1 2
1 2 3
Enter the number of rows for pattern 2: 3
*
* *
* * *
Enter the number of rows for pattern 3: 3
1
2 3
4 5 6
Enter the number of rows for pattern 4: 4
1
2 2
3 3 3
4 4 4 4
Enter the widest row for pattern 5: 3
*
* *
* * *
* *
* Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
The syllabus shows the five patterns with different heights: three rows for the first three, four rows for the fourth, and a fifth pattern whose widest row has three stars. So the program reads a separate row count for each pattern, and the run uses 3, 3, 3, 4 and 3 to give exactly the syllabus shapes. For pattern 5 the number read is the widest row, so 3 gives 1, 2, 3, 2, 1 stars (5 rows in all).
Every number and star is followed by a space so that two-digit numbers stay readable (pattern 3 reaches 10 at its fourth row). To print them joined, as the syllabus writes 123, change "%d " to "%d" and "* " to "*".
All five patterns use the same idea: an outer loop for the rows and an inner loop that runs i times in row i. Only what is printed inside the inner loop changes.
Common mistakes
Putting
printf("\n")inside the inner loop. Each number then goes on its own line. The newline belongs after the inner loop, once per row.Resetting
numto 1 inside the outer loop in pattern 3. Then every row starts again from 1 and you get pattern 1 instead.nummust be set once, before the loops.Printing
jinstead ofiin pattern 4. That gives 1 / 1 2 / 1 2 3 instead of 1 / 2 2 / 3 3 3.In pattern 5, starting the second loop at
widestinstead ofwidest - 1. The widest row is then printed twice.
Viva questions
Try to answer each one aloud before you open it.
In these patterns, what do the outer loop and the inner loop control?
The outer loop controls the rows. The inner loop controls what is printed in one row, and here it runs i times in row i.
How many numbers are printed in total in pattern 3 with n rows?
1 + 2 + ... + n = n(n + 1) / 2. For 3 rows that is 6, so the last number is 6.
What is the difference between pattern 1 and pattern 4?
Pattern 1 prints the column number
j, so each row counts 1, 2, 3. Pattern 4 prints the row numberi, so the same number repeats across the row.How many rows does pattern 5 print when the widest row is w?
w rows going up and w − 1 rows coming down, so 2w − 1 rows. For w = 3 that is 5 rows.
What is a nested loop?
A loop inside another loop. For every single pass of the outer loop, the inner loop runs completely.