-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascending_triangle.c
62 lines (51 loc) · 1.89 KB
/
ascending_triangle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/**
* Prints an ascending number triangle with the specified number of rows.
* Each row contains increasing consecutive numbers starting from 1.
*/
void printAscendingTriangle(int rows) {
int row, col, num = 1; // Initialize row, column, and starting number
// Outer loop for each row
for(row = 1; row <= rows; row++) {
// Inner loop for each column in the current row
for(col = 1; col <= row; col++) {
printf("%2d ", num++); // Print the current number with alignment
}
printf("\n"); // Move to the next line after each row
}
}
// Function to clear input buffer
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main() {
int rows;
char choice = 'y';
do {
// Get user input
printf("\nEnter the number of rows for the ascending triangle: ");
if (scanf("%d", &rows) != 1) {
printf("\nError: Invalid input. Please enter a valid integer.\n");
clearInputBuffer();
continue; // Skip to next iteration
}
clearInputBuffer(); // Clear buffer after successful read
// Input validation
if (rows <= 0) {
printf("\nError: Number of rows must be greater than zero.\n");
continue; // Skip to next iteration
}
printf("\nAscending Number Triangle:\n\n");
// Call the function to print the triangle
printAscendingTriangle(rows);
// Ask user if they want to continue
printf("\nDo you want to generate another triangle? (y/n): ");
scanf(" %c", &choice);
clearInputBuffer();
} while (tolower(choice) == 'y');
printf("\nThank you for using the triangle generator!\n\n");
return 0; // Indicate successful program termination
}