{"id":6253,"date":"2025-05-30T20:37:08","date_gmt":"2025-05-30T15:07:08","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6253"},"modified":"2025-05-30T20:37:08","modified_gmt":"2025-05-30T15:07:08","slug":"star-pattern-programs-in-javascript-java-c-c-and-python","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/star-pattern-programs-in-javascript-java-c-c-and-python\/","title":{"rendered":"Star Pattern Programs in JavaScript, Java, C++, C, and Python"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n  }\n \n \n  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n  }\n \n \n  .wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242B33 !important;\n  }\n \n \n  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n \n \n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242B33 !important;\n  }\n \n \n  .wp_blog_code-tab-content > pre{\n    background: #242B33 !important;\n  }\n \n \n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n \n \n  .wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n  }\n \n \n  .wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n color : black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n  }\n \n \n  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n  }\n \n \n  .wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n  }\n \n \n .wp_blog_explanation h1,\n .wp_blog_explanation h2,\n .wp_blog_explanation h3,\n .wp_blog_explanation h4,\n .wp_blog_explanation h5,\n .wp_blog_explanation h6,\n .wp_blog_explanation p {\n  margin-top: 10px !important;\n  margin-bottom: 10px !important;\n }\n <\/style>\n \n<div class=\"wp_blog_explanation\">\n  <h2>Pattern 1 &#8211; Print n x n Star Square<\/h2>\n  <p>Print a square pattern of stars (<code>*<\/code>) of size <code>n x n<\/code>.<\/p>\n  <h2>Example Output<\/h2>\n  <pre><code>\n****\n****\n****\n****<\/code><\/pre>\n  <h2>Approach<\/h2>\n  <ul>\n    <li><strong>Outer Loop (Rows):<\/strong> Run from <code>i = 0<\/code> to <code>i = n - 1<\/code>.<\/li>\n    <li><strong>Inner Loop (Columns):<\/strong> For each row, loop from <code>j = 0<\/code> to <code>j = n - 1<\/code>.<\/li>\n    <li><strong>Build Row String:<\/strong> Append <code>*<\/code> in each inner loop iteration.<\/li>\n    <li><strong>Print Row:<\/strong> After the inner loop, print the complete row string.<\/li>\n  <\/ul>\n\n  <h2>Time &#038; Space Complexity<\/h2>\n  <ul>\n    <li><strong>Time Complexity:<\/strong> <code>O(n^2)<\/code><\/li>\n    <li><strong>Space Complexity:<\/strong> <code>O(n)<\/code> (temporary row string)<\/li>\n  <\/ul>\n\n\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">let n = 4;\nfor (let i = 0; i < n; i++) {\n  let row = \"\";\n  for (let j = 0; j < n; j++) {\n    row += \"*\";\n  }\n  console.log(row);\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class StarSquare {\n  public static void main(String[] args) {\n    int n = 4;\n    for (int i = 0; i < n; i++) {\n      String row = \"\";\n      for (int j = 0; j < n; j++) {\n        row += \"*\";\n      }\n      System.out.println(row);\n    }\n  }\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    string row = \"\";\n    for (int j = 0; j < n; j++) {\n      row += \"*\";\n    }\n    cout << row << endl;\n  }\n  return 0;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j < n; j++) {\n      printf(\"*\");\n    }\n    printf(\"\\n\");\n  }\n  return 0;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">n = 4\nfor i in range(n):\n  row = \"\"\n  for j in range(n):\n    row += \"*\"\n  print(row)<\/code><\/pre>\n  <\/div>\n<\/div>\n<div class=\"wp_blog_explanation\">\n  <h2>Pattern 2 - Right-Angled Star Triangle Pattern<\/h2>\n  <p>Write a program to print a right-angled triangle of stars (<code>*<\/code>) with <code>n<\/code> rows.<\/p>\n  <h2>Example Output<\/h2>\n  <pre><code>\n*\n**\n***\n****\n  <\/code><\/pre>\n  <h2>Approach to Print Right-Angled Star Triangle<\/h2>\n  <ul>\n    <li><strong>Outer Loop (Rows):<\/strong> Run a loop from <code>i = 0<\/code> to <code>i = n - 1<\/code>. Each iteration represents one row.<\/li>\n    <li><strong>Inner Loop (Stars per Row):<\/strong> For each row <code>i<\/code>, run another loop from <code>j = 0<\/code> to <code>j = i<\/code> and append a <code>*<\/code> character to a string.<\/li>\n    <li><strong>Print Row:<\/strong> Print the string after the inner loop completes for each row.<\/li>\n  <\/ul>\n\n  <h2>Time & Space Complexity<\/h2>\n  <ul>\n    <li><strong>Time Complexity:<\/strong> <code>O(n^2)<\/code> because the total number of stars printed is <code>1 + 2 + ... + n = n(n+1)\/2<\/code>.<\/li>\n    <li><strong>Space Complexity:<\/strong> <code>O(n)<\/code> for the temporary string variable storing each row.<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">let n = 4;\nfor (let i = 0; i < n; i++) {\n  let row = \"\";\n  for (let j = 0; j <= i; j++) {\n    row += \"*\";\n  }\n  console.log(row);\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class StarTriangle {\n  public static void main(String[] args) {\n    int n = 4;\n    for (int i = 0; i < n; i++) {\n      String row = \"\";\n      for (int j = 0; j <= i; j++) {\n        row += \"*\";\n      }\n      System.out.println(row);\n    }\n  }\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    string row = \"\";\n    for (int j = 0; j <= i; j++) {\n      row += \"*\";\n    }\n    cout << row << endl;\n  }\n  return 0;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j <= i; j++) {\n      printf(\"*\");\n    }\n    printf(\"\\n\");\n  }\n  return 0;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">n = 4\nfor i in range(n):\n    row = \"\"\n    for j in range(i + 1):\n        row += \"*\"\n    print(row)<\/code><\/pre>\n  <\/div>\n<\/div>\n\n<div class=\"wp_blog_explanation\">\n  <h2>Pattern 3 - Print a Right-Angled Number Triangle<\/h2>\n  <p>Write a program that prints a right-angled triangle of numbers of height <code>n<\/code>.<\/p>\n\n  <h2>Example Output<\/h2>\n  <pre><code>\n1\n12\n123\n1234\n  <\/code><\/pre>\n\n  <h2>Step-by-Step Approach<\/h2>\n  <ul>\n    <li><strong>Outer Loop (Rows):<\/strong> Run a loop from <code>i = 0<\/code> to <code>i &lt; n<\/code>. Each iteration represents a new row.<\/li>\n    <li><strong>Inner Loop (Numbers):<\/strong> Run an inner loop from <code>j = 0<\/code> to <code>j &lt;= i<\/code>, and append <code>j + 1<\/code> to the row.<\/li>\n    <li><strong>Build and Print:<\/strong> Construct a string for the row and print it after the inner loop ends.<\/li>\n  <\/ul>\n\n  <h2>Time & Space Complexity<\/h2>\n  <ul>\n    <li>Time Complexity: <code>O(n\u00b2)<\/code> \u2014 Each row can have up to <code>n<\/code> numbers.<\/li>\n    <li>Space Complexity: <code>O(n)<\/code> \u2014 Temporary string to build each row.<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">let n = 4;\nfor (let i = 0; i < n; i++) {\n  let row = \"\";\n  for (let j = 0; j <= i; j++) {\n    row += (j + 1);\n  }\n  console.log(row);\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class NumberTriangle {\n  public static void main(String[] args) {\n    int n = 4;\n    for (int i = 0; i < n; i++) {\n      String row = \"\";\n      for (int j = 0; j <= i; j++) {\n        row += (j + 1);\n      }\n      System.out.println(row);\n    }\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    string row = \"\";\n    for (int j = 0; j <= i; j++) {\n      row += to_string(j + 1);\n    }\n    cout &lt;&lt; row &lt;&lt; endl;\n  }\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j <= i; j++) {\n      printf(\"%d\", j + 1);\n    }\n    printf(\"\\n\");\n  }\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">n = 4\nfor i in range(n):\n    row = \"\"\n    for j in range(i + 1):\n        row += str(j + 1)\n    print(row)\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n\n<div class=\"wp_blog_explanation\">\n  <h2>Pattern 4 - Print a Right-Angled Triangle of Repeated Numbers<\/h2>\n  <p>Write a program that prints a right-angled triangle where each row contains the same number repeated.<\/p>\n\n  <h2>Example Output<\/h2>\n  <pre><code>\n1\n22\n333\n4444\n  <\/code><\/pre>\n\n  <h2>Step-by-Step Approach<\/h2>\n  <ul>\n    <li><strong>Outer Loop (Rows):<\/strong> Loop from <code>i = 0<\/code> to <code>i &lt; n<\/code>.<\/li>\n    <li><strong>Inner Loop (Repeated Numbers):<\/strong> Loop from <code>j = 0<\/code> to <code>j &lt;= i<\/code>, appending <code>i + 1<\/code> as a string.<\/li>\n    <li><strong>Build and Print:<\/strong> Build the row string and print it.<\/li>\n  <\/ul>\n\n  <h2>Time & Space Complexity<\/h2>\n  <ul>\n    <li>Time Complexity: <code>O(n\u00b2)<\/code><\/li>\n    <li>Space Complexity: <code>O(n)<\/code> \u2014 for the temporary row string<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">let n = 4;\nfor (let i = 0; i < n; i++) {\n  let row = \"\";\n  for (let j = 0; j <= i; j++) {\n    row += (i + 1);\n  }\n  console.log(row);\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class NumberRepeatedTriangle {\n  public static void main(String[] args) {\n    int n = 4;\n    for (int i = 0; i < n; i++) {\n      String row = \"\";\n      for (int j = 0; j <= i; j++) {\n        row += (i + 1);\n      }\n      System.out.println(row);\n    }\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    string row = \"\";\n    for (int j = 0; j <= i; j++) {\n      row += to_string(i + 1);\n    }\n    cout &lt;&lt; row &lt;&lt; endl;\n  }\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j <= i; j++) {\n      printf(\"%d\", i + 1);\n    }\n    printf(\"\\n\");\n  }\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">n = 4\nfor i in range(n):\n    row = \"\"\n    for j in range(i + 1):\n        row += str(i + 1)\n    print(row)\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n<div class=\"wp_blog_explanation\">\n  <h2>Pattern 5 - Print a Reverse Right-Angled Triangle of Increasing Numbers<\/h2>\n  <p>Write a program that prints a reverse right-angled triangle where each row starts from 1 and the number of elements decreases with each row.<\/p>\n\n  <h2>Example Output (n = 4)<\/h2>\n  <pre><code>\n1234\n123\n12\n1\n  <\/code><\/pre>\n\n  <h2>Approach<\/h2>\n  <ul>\n    <li><strong>Outer Loop (Rows):<\/strong> Loop <code>i<\/code> from <code>0<\/code> to <code>n - 1<\/code>. Each iteration represents a row.<\/li>\n    <li><strong>Inner Loop (Print Numbers):<\/strong> For each row, loop <code>j<\/code> from <code>0<\/code> to <code>n - i - 1<\/code> and append <code>j + 1<\/code> to a row string.<\/li>\n    <li><strong>Print Row:<\/strong> After the inner loop, print the row string.<\/li>\n  <\/ul>\n\n  <h2>Time and Space Complexity<\/h2>\n  <ul>\n    <li>Time Complexity: <code>O(n\u00b2)<\/code><\/li>\n    <li>Space Complexity: <code>O(n)<\/code> for the temporary row string<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">let n = 4;\nfor (let i = 0; i < n; i++) {\n  let row = \"\";\n  for (let j = 0; j < n - i; j++) {\n    row += (j + 1);\n  }\n  console.log(row);\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class ReverseNumberTriangle {\n  public static void main(String[] args) {\n    int n = 4;\n    for (int i = 0; i < n; i++) {\n      String row = \"\";\n      for (int j = 0; j < n - i; j++) {\n        row += (j + 1);\n      }\n      System.out.println(row);\n    }\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    string row = \"\";\n    for (int j = 0; j < n - i; j++) {\n      row += to_string(j + 1);\n    }\n    cout &lt;&lt; row &lt;&lt; endl;\n  }\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j < n - i; j++) {\n      printf(\"%d\", j + 1);\n    }\n    printf(\"\\n\");\n  }\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">n = 4\nfor i in range(n):\n    row = \"\"\n    for j in range(n - i):\n        row += str(j + 1)\n    print(row)\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n\n<div class=\"wp_blog_explanation\">\n  <h2>Pattern 6 - Print a Right-Aligned Right-Angled Triangle of Stars<\/h2>\n  <p>Write a program that prints a right-aligned triangle of stars increasing row by row, with leading spaces for alignment.<\/p>\n\n  <h2>Example Output (n = 4)<\/h2>\n  <pre><code>\n   *\n  **\n ***\n****\n  <\/code><\/pre>\n\n  <h2>Approach<\/h2>\n  <ol>\n    <li><strong>Outer Loop (Rows):<\/strong> Loop <code>i<\/code> from <code>0<\/code> to <code>n - 1<\/code>. Each iteration is a new row.<\/li>\n    <li><strong>Inner Loop 1 (Spaces):<\/strong> For each row, add <code>n - i - 1<\/code> spaces before the stars to right-align the triangle.<\/li>\n    <li><strong>Inner Loop 2 (Stars):<\/strong> Add <code>i + 1<\/code> stars after the spaces.<\/li>\n    <li><strong>Print Row:<\/strong> Combine the spaces and stars, then print the row.<\/li>\n  <\/ol>\n\n  <h2>Time and Space Complexity<\/h2>\n  <ul>\n    <li>Time Complexity: <code>O(n\u00b2)<\/code><\/li>\n    <li>Space Complexity: <code>O(n)<\/code> for the row string<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">let n = 4;\nfor (let i = 0; i < n; i++) {\n  let row = \"\";\n  for (let j = 0; j < n - (i + 1); j++) {\n    row += \" \";\n  }\n  for (let k = 0; k < i + 1; k++) {\n    row += \"*\";\n  }\n  console.log(row);\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class RightAlignedTriangle {\n  public static void main(String[] args) {\n    int n = 4;\n    for (int i = 0; i < n; i++) {\n      String row = \"\";\n      for (int j = 0; j < n - (i + 1); j++) {\n        row += \" \";\n      }\n      for (int k = 0; k < i + 1; k++) {\n        row += \"*\";\n      }\n      System.out.println(row);\n    }\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    string row = \"\";\n    for (int j = 0; j < n - (i + 1); j++) {\n      row += \" \";\n    }\n    for (int k = 0; k < i + 1; k++) {\n      row += \"*\";\n    }\n    cout &lt;&lt; row &lt;&lt; endl;\n  }\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j < n - (i + 1); j++) {\n      printf(\" \");\n    }\n    for (int k = 0; k < i + 1; k++) {\n      printf(\"*\");\n    }\n    printf(\"\\n\");\n  }\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">n = 4\nfor i in range(n):\n    row = \"\"\n    for j in range(n - (i + 1)):\n        row += \" \"\n    for k in range(i + 1):\n        row += \"*\"\n    print(row)\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n<div class=\"wp_blog_explanation\">\n  <h2>Pattern 7 - Print a Right-Angled Triangle of Alternating 1s and 0s<\/h2>\n  <p>Write a program that prints a triangle of alternating 1s and 0s starting with 1 on each row.<\/p>\n\n  <h2>Example Output (n = 4)<\/h2>\n  <pre><code>\n1\n10\n101\n1010\n  <\/code><\/pre>\n\n  <h2>Approach<\/h2>\n  <ol>\n    <li><strong>Outer Loop (Rows):<\/strong> Loop <code>i<\/code> from <code>0<\/code> to <code>n - 1<\/code>.<\/li>\n    <li><strong>Initialize toggle = 1:<\/strong> Start each row with <code>toggle = 1<\/code>.<\/li>\n    <li><strong>Inner Loop (Columns):<\/strong> For each row, loop <code>j<\/code> from <code>0<\/code> to <code>i<\/code>. On each iteration:\n      <ul>\n        <li>Append <code>toggle<\/code> to the row string.<\/li>\n        <li>Flip <code>toggle<\/code> between 1 and 0.<\/li>\n      <\/ul>\n    <\/li>\n    <li><strong>Print Row:<\/strong> After inner loop, print the row string.<\/li>\n  <\/ol>\n\n  <h2>Time and Space Complexity<\/h2>\n  <ul>\n    <li>Time Complexity: <code>O(n\u00b2)<\/code><\/li>\n    <li>Space Complexity: <code>O(n)<\/code> per row<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">let n = 4;\nfor (let i = 0; i < n; i++) {\n  let row = \"\";\n  let toggle = 1;\n  for (let j = 0; j < i + 1; j++) {\n    row += toggle;\n    toggle = toggle === 1 ? 0 : 1;\n  }\n  console.log(row);\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class BinaryTriangle {\n  public static void main(String[] args) {\n    int n = 4;\n    for (int i = 0; i < n; i++) {\n      String row = \"\";\n      int toggle = 1;\n      for (int j = 0; j < i + 1; j++) {\n        row += toggle;\n        toggle = (toggle == 1) ? 0 : 1;\n      }\n      System.out.println(row);\n    }\n  }\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    string row = \"\";\n    int toggle = 1;\n    for (int j = 0; j < i + 1; j++) {\n      row += to_string(toggle);\n      toggle = (toggle == 1) ? 0 : 1;\n    }\n    cout &lt;&lt; row &lt;&lt; endl;\n  }\n  return 0;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int n = 4;\n  for (int i = 0; i < n; i++) {\n    int toggle = 1;\n    for (int j = 0; j < i + 1; j++) {\n      printf(\"%d\", toggle);\n      toggle = (toggle == 1) ? 0 : 1;\n    }\n    printf(\"\\n\");\n  }\n  return 0;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">n = 4\nfor i in range(n):\n    row = \"\"\n    toggle = 1\n    for j in range(i + 1):\n        row += str(toggle)\n        toggle = 0 if toggle == 1 else 1\n    print(row)<\/code><\/pre>\n  <\/div>\n<\/div>\n<div class=\"wp_blog_explanation\">\n  <h2>Pattern 8 - Right-Angled Triangle of Alternating 1s and 0s (Global Toggle)<\/h2>\n  <p>Write a program to print a triangle of alternating 1s and 0s, but the toggle continues globally across rows.<\/p>\n\n  <h2>Output (n = 4)<\/h2>\n  <pre><code>\n1\n01\n010\n1010\n  <\/code><\/pre>\n\n  <h2>Approach<\/h2>\n  <ol>\n    <li><strong>Global Toggle Variable:<\/strong> Declare <code>toggle = 1<\/code> before the outer loop.<\/li>\n    <li><strong>Outer Loop:<\/strong> Loop <code>i<\/code> from <code>0<\/code> to <code>n - 1<\/code>.<\/li>\n    <li><strong>Inner Loop:<\/strong> Loop <code>j<\/code> from <code>0<\/code> to <code>i<\/code>. On each iteration:\n      <ul>\n        <li>Append <code>toggle<\/code> to the row string.<\/li>\n        <li>Flip <code>toggle<\/code>: <code>1 \u2192 0<\/code> and <code>0 \u2192 1<\/code>.<\/li>\n      <\/ul>\n    <\/li>\n    <li><strong>Print the row<\/strong> after the inner loop.<\/li>\n  <\/ol>\n\n  <h2>Key Difference<\/h2>\n  <p>In the previous pattern, <code>toggle = 1<\/code> was reset each row. Here, the toggle continues globally across the entire pattern.<\/p>\n\n  <h2>Time & Space Complexity<\/h2>\n  <ul>\n    <li><strong>Time Complexity:<\/strong> <code>O(n\u00b2)<\/code><\/li>\n    <li><strong>Space Complexity:<\/strong> <code>O(n)<\/code> per row<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">let n = 4;\nlet toggle = 1;\nfor (let i = 0; i < n; i++) {\n  let row = \"\";\n  for (let j = 0; j < i + 1; j++) {\n    row += toggle;\n    toggle = toggle === 1 ? 0 : 1;\n  }\n  console.log(row);\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class GlobalToggleTriangle {\n  public static void main(String[] args) {\n    int n = 4;\n    int toggle = 1;\n    for (int i = 0; i < n; i++) {\n      String row = \"\";\n      for (int j = 0; j < i + 1; j++) {\n        row += toggle;\n        toggle = (toggle == 1) ? 0 : 1;\n      }\n      System.out.println(row);\n    }\n  }\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 4;\n  int toggle = 1;\n  for (int i = 0; i < n; i++) {\n    string row = \"\";\n    for (int j = 0; j < i + 1; j++) {\n      row += to_string(toggle);\n      toggle = (toggle == 1) ? 0 : 1;\n    }\n    cout &lt;&lt; row &lt;&lt; endl;\n  }\n  return 0;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int n = 4;\n  int toggle = 1;\n  for (int i = 0; i &lt; n; i++) {\n    for (int j = 0; j &lt; i + 1; j++) {\n      printf(\"%d\", toggle);\n      toggle = (toggle == 1) ? 0 : 1;\n    }\n    printf(\"\\n\");\n  }\n  return 0;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">n = 4\ntoggle = 1\nfor i in range(n):\n    row = \"\"\n    for j in range(i + 1):\n        row += str(toggle)\n        toggle = 0 if toggle == 1 else 1\n    print(row)<\/code><\/pre>\n  <\/div>\n<\/div>\n<script>\ndocument.addEventListener('DOMContentLoaded', function () {\n  const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n  const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n\n  buttons.forEach(button => {\n    button.addEventListener('click', () => {\n      const lang = button.getAttribute('data-lang');\n\n      buttons.forEach(btn => btn.classList.remove('active'));\n      button.classList.add('active');\n\n      contents.forEach(content => {\n        content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n      });\n    });\n  });\n});\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Pattern 1 &#8211; Print n x n Star Square Print a square pattern of stars (*) of size n x n. Example Output **** **** **** **** Approach Outer Loop (Rows): Run from i = 0 to i = n &#8211; 1. Inner Loop (Columns): For each row, loop from j = 0 to j<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,811,810,174,172,173],"tags":[],"class_list":["post-6253","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6253"}],"version-history":[{"count":3,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6253\/revisions"}],"predecessor-version":[{"id":7140,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6253\/revisions\/7140"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}