Basic Double Incrementing Triangle Pattern (init = 3)
Program to Print Basic Double Incrementing Triangle Pattern (init = 3)
PRINTING PATTERN:
3 4 5 6 7 8 9 10 11 12
PREREQUISITE:
Basic knowledge of C language and loops.
ALGORITHM:
1. Initialize a variable (let's call it 'init') to 3. This will be the starting number for the pattern.
2. Initialize another variable (let's call it 'num') to 'init'. This variable will be used to print the numbers in the pattern.
3. Initialize a loop that runs from 1 to 'init' (inclusive) to iterate through each row.
4. For each row (let's call the current row index 'i'), initialize another loop that runs from 1 to 'i' (inclusive) to print the numbers in that row.
5. Inside the inner loop, print the current value of 'num', and then increment 'num' by 1.
6. After finishing the inner loop for a row, print a newline character to move to the next row.
7. Repeat steps 3 to 6 until all rows are printed.
Code in C:
#include <stdio.h> int main() { int init = 3; // Starting number int num = init; // Variable to keep track of the current number int i, j; // Loop counters // Loop to print the double incrementing triangle pattern for (i = 1; i <= init; i++) { // Loop for each row for (j = 1; j <= i; j++) { // Loop for each column in the current row printf("%d ", num); // Printing the current number num++; // Incrementing the number } printf("\n"); // New line after each row } return 0; }
Output:
3 4 5 6 7 8 9 10 11 12