C Online Compiler
Example: Pascal's Triangle using Optimized Iteration in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Pascal's Triangle using Optimized Iteration #include <stdio.h> int main() { int rows = 5; // Number of rows to generate printf("Pascal's Triangle (Optimized Iterative approach):\n"); // Step 1: Iterate through each row for (int i = 0; i < rows; i++) { // Step 2: Print leading spaces for (int space = 0; space < rows - i - 1; space++) { printf(" "); } long long current_val = 1; // The first element in any row is 1 // Step 3: Iterate through each element in the current row for (int j = 0; j <= i; j++) { printf("%lld ", current_val); // Print the current element // Step 4: Calculate the next element using the formula: // C(i, j) = C(i, j-1) * (i - j + 1) / j current_val = current_val * (i - j) / (j + 1); } printf("\n"); // Move to the next line } return 0; }
Output
Clear
ADVERTISEMENTS