V tomto tématu se naučíme, jak vytvářet vzory pomocí jazyka C. Vzory vytvoříme buď pomocí znaku hvězdičky '*' nebo jiného znaku. Vytvoříme různé vzory nebo geometrický tvar jako trojúhelník, čtverec atd.
Budeme se zabývat následujícími vzory. Kód libovolného vzoru uvedeného níže můžete zobrazit kliknutím na vzor.
čtvercový vzor hvězdy
Kód pro vytvoření vzoru čtvercové hvězdy je uveden níže:
#include int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=0;i<n;i++) { for(int j="0;j<n;j++)" printf('*'); } printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-26.webp" alt="Star Program in C"> <h2 id="Hollow-Square-Star">Hollow Square Star Pattern</h2> <p>Now, we create the hollow square star pattern. The source code for this pattern is given below:</p> <pre> int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=1;i<=n;i++) { for(int j="1;j<=n;j++)" if(i="=1" ||i="=n||j==1||j==n)" printf('*'); } else printf(' '); printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-27.webp" alt="Star Program in C"> <h2 id="Hollow-Square-Pattern">Hollow Square Pattern with Diagonal</h2> <p>The code for the hollow square star pattern is given below:</p> <pre> #include int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=1;i<=n;i++) { for(int j="1;j<=n;j++)" if(i="=1" ||i="=n||j==1||j==n-i+1||i==j||j==n)" printf('*'); } else printf(' '); printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-28.webp" alt="Star Program in C"> <h2 id="Rhombus-Star">Rhombus Star Pattern</h2> <p>The code for the rhombus star pattern is given below:</p> <pre> #include int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=n;i>=1;i--) { for(int j=1;j<=i-1;j++) { printf(' '); } for(int k="1;k<=n;k++)" printf('*'); printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-29.webp" alt="Star Program in C"> <h2 id="Hollow-Rhombus-Star">Hollow Rhombus Star Pattern</h2> <p>The code for the hollow rhombus star pattern is given below:</p> <pre> #include int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=n;i>=1;i--) { for(int j=1;j<=i-1;j++) { printf(' '); } for(int k="1;k<=n;k++)" printf('*'); else printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-30.webp" alt="Star Program in C"> <h2 id="Mirrored-Rhombus-Star">Mirrored Rhombus Star Pattern</h2> <p>The code for the mirrored rhombus star pattern is given below:</p> <pre> #include int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=1;i<=n;i++) { for(int j="1;j" <i;j++) printf(' '); } k="1;k<=n;k++)" printf('*'); printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-31.webp" alt="Star Program in C"> <h2 id="Hollow-Mirrored-Rhombus-Star">Hollow Mirrored Rhombus Star Pattern</h2> <p>The code for the hollow mirrored rhombus star pattern is given below:</p> <pre> #include int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=1;i<=n;i++) { for(int j="1;j" <i;j++) printf(' '); } k="1;k<=n;k++)" printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-32.webp" alt="Star Program in C"> <h2 id="Right-Triangle-Star">Right Triangle Star Pattern</h2> <p>The code for the right triangle star pattern is given below:</p> <pre> #include int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=1;i<=n;i++) { for(int j="1;j<=i;j++)" printf('* '); } printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-33.webp" alt="Star Program in C"> <h2 id="Hollow-Right-Triangle-Star">Hollow Right Triangle Star Pattern</h2> <p>The code for the hollow right triangle star pattern is given below:</p> <pre> #include int main() { int n; printf('Enter the number of rows'); scanf('%d',&n); for(int i=1;i<=n;i++) { for(int j="1;j<=i;j++)" if(j="=1||" i="=j" || ) printf('*'); } else printf(' '); printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-34.webp" alt="Star Program in C"> <h2 id="Mirrored-Right-Triangle-Star">Mirrored Right Triangle Star Pattern</h2> <p>The code for the mirrored right triangle star pattern is given below:</p> <pre> #include int main() { int n,m=1; printf('Enter the number of rows'); scanf('%d',&n); for(int i=n;i>=1;i--) { for(int j=1;j<=i-1;j++) { printf(' '); } for(int k="1;k<=m;k++)" printf('*'); printf(' '); m++; return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-35.webp" alt="Star Program in C"> <h2 id="Hollow-Mirrored-Right-Triangle-Star">Hollow Mirrored Right Triangle Star Pattern</h2> <p>The code for the hollow mirrored right triangle star pattern is given below:</p> <pre> #include int main() { int n,m=1; printf('Enter the number of rows'); scanf('%d',&n); for(int i=n;i>=1;i--) { for(int j=1;j<=i-1;j++) { printf(' '); } for(int k="1;k<=m;k++)" m="=n)" printf('*'); else printf(' '); m++; return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-36.webp" alt="Star Program in C"> <h2 id="Inverted-Right-Triangle-Star">Inverted Right Triangle Star Pattern</h2> <p>The code for the inverted right triangle star pattern is given below:</p> <pre> #include int main() { int n,m=1; printf('Enter the number of rows'); scanf('%d',&n); for(int i=n;i>=1;i--) { for(int j=1;j<=i;j++) { printf('*'); } printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-37.webp" alt="Star Program in C"> <h2 id="Hollow-Inverted-Right-Triangle-Star">Hollow Inverted Right Triangle Star Pattern</h2> <p>The code for the hollow inverted right triangle star pattern is given below:</p> <pre> #include int main() { int n,m=1; printf('Enter the number of rows'); scanf('%d',&n); for(int i=n;i>=1;i--) { for(int j=1;j<=i;j++) i="=n)" printf('*'); else printf(' '); printf(' '); } return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-38.webp" alt="Star Program in C"> <h2 id="Inverted-Mirrored-Right-Triangle-Star">Inverted Mirrored Right Triangle Star Pattern</h2> <p>The code for the inverted mirrored right triangle star pattern is given below:</p> <pre> #include int main() { int n,m; printf('Enter the number of rows'); scanf('%d',&n); m=n; for(int i=1;i<=n;i++) { for(int j="1;j" <i;j++) printf(' '); } k="1;k<=m;k++)" printf('*'); m--; printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-39.webp" alt="Star Program in C"> <h2 id="Hollow-Inverted-Mirrored-Right-Triangle-Star">Hollow Inverted Mirrored Right Triangle Star Pattern</h2> <p>The code for the hollow inverted mirrored right triangle star pattern is given below:</p> <pre> #include int main() { int n,m; printf('Enter the number of rows'); scanf('%d',&n); m=n; for(int i=1;i<=n;i++) { for(int j="1;j" <i;j++) printf(' '); } k="1;k<=m;k++)" if(i="=1" m--; printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-40.webp" alt="Star Program in C"> <h2 id="Pyramid-Star">Pyramid Star Pattern</h2> <p>The code for the pyramid star pattern is given below:</p> <pre> #include int main() { int n,m; printf('Enter the number of rows'); scanf('%d',&n); m=n; for(int i=1;i<=n;i++) { for(int j="1;j<=m-1;j++)" printf(' '); } k="1;k<=2*i-1;k++)" printf('*'); m--; printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-41.webp" alt="Star Program in C"> <h2 id="Hollow-Pyramid">Hollow Pyramid Star Pattern</h2> <p>The code for the hollow pyramid star pattern is given below:</p> <pre> #include int main() { int n,m; printf('Enter the number of rows'); scanf('%d',&n); m=n; for(int i=1;i<=n;i++) { for(int j="1;j<=m-1;j++)" printf(' '); } k="1;k<=2*i-1;k++)" if(k="=1" m--; printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-42.webp" alt="Star Program in C"> <h2 id="Inverted-Pyramid-Star">Inverted Pyramid Star Pattern</h2> <p>The code for the inverted pyramid is given below:</p> <pre> #include int main() { int n,m=1; printf('Enter the number of rows'); scanf('%d',&n); for(int i=n;i>=1;i--) { for(int j=1;j<m;j++) { printf(' '); } for(int k="1;k<=2*i-1;k++)" printf('*'); m++; printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-43.webp" alt="Star Program in C"> <h2 id="Hollow-Pyramid-Star">Hollow Pyramid Star Pattern</h2> <p>The code for the hollow pyramid star pattern is given below:</p> <pre> #include int main() { int n,m=1; printf('Enter the number of rows'); scanf('%d',&n); for(int i=n;i>=1;i--) { for(int j=1;j<m;j++) { printf(' '); } for(int k="1;k<=2*i-1;k++)" m++; printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-44.webp" alt="Star Program in C"> <h2 id="Half-Diamond-Star">Half Diamond Star Pattern</h2> <p>The code for the half diamond star pattern is given below:</p> <pre> #include int main() { int n,m=1; printf('Enter the number of columns'); scanf('%d',&n); for(int i=1;i<=n;i++) { for(int j="1;j=1;i--)" printf('*'); } printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-45.webp" alt="Star Program in C"> <h2 id="Diamond-Star">Diamond Star Pattern</h2> <p>The code for the diamond star pattern is given below:</p> <pre> #include int main(void) { int n; printf('Enter the number of rows '); scanf('%d',&n); int spaces=n-1; int stars=1; for(int i=1;i<=n;i++) { for(int j="1;j<=spaces;j++)" printf(' '); } k="1;ki)" spaces="spaces-1;" stars="stars+2;" if(spaces <i) printf(' '); return 0;} < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-46.webp" alt="Star Program in C"> <h2 id="Right-Arrow-Star">Right Arrow Star Pattern</h2> <p>The code of the right arrow star pattern is given below:</p> <pre> #include int main(void) { int n; printf('Enter the number of columns'); scanf('%d',&n); //printing the upper part of the pattern.. for(int i=0;i<n;i++) { for(int j="0;j" <i;j++) printf(' '); } k="1;k<=n-i;k++)" printf('*'); printf(' '); i="1;i<n;i++)" return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-47.webp" alt="Star Program in C"> <h2 id="Left-Arrow-Star">Left Arrow Star Pattern</h2> <p>The code for the Left arrow star pattern is given below:</p> <pre> #include int main(void) { int n; printf('Enter the number of columns'); scanf('%d',&n); //printing the upper part of the pattern.. for(int i=1;i<=n;i++) { for(int j="1;j<=n-i;j++)" printf(' '); } k="0;k<=n-i;k++)" printf('*'); printf(' '); i="1;i<n;i++)" <i+1;j++) return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-48.webp" alt="Star Program in C"> <h2 id="Plus-Star">Plus Star Pattern</h2> <p>The code for the plus star pattern is given below:</p> <pre> #include int main(void) { int n; printf('Enter the odd number only'); scanf('%d', &n); for(int i=1;i<=n;i++) { if(i="=((n/2)+1))" for(int j="1;j<=n;j++)" printf('+'); } else printf(' '); printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-49.webp" alt="Star Program in C"> <h2 id="X-Star">X Star Pattern</h2> <p>The code for the X star pattern is given below:</p> <pre> #include int main(void) { int n,m; printf('Enter the number'); scanf('%d',&n); m=2*n-1; for(int i=1;i<=m;i++) { for(int j="1;j<=m;j++)" if(i="=j" || printf('*'); } else printf(' '); printf(' '); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/star-patterns-program-c-50.webp" alt="Star Program in C"> <hr></=m;i++)></pre></=n;i++)></pre></=n;i++)></pre></n;i++)></pre></=n;i++)></pre></=n;i++)></pre></m;j++)></pre></m;j++)></pre></=n;i++)></pre></=n;i++)></pre></=n;i++)></pre></=n;i++)></pre></=i;j++)></pre></=i;j++)></pre></=i-1;j++)></pre></=i-1;j++)></pre></=n;i++)></pre></=n;i++)></pre></=n;i++)></pre></=n;i++)></pre></=i-1;j++)></pre></=i-1;j++)></pre></=n;i++)></pre></=n;i++)></pre></n;i++)>=n;i++)>=n;i++)>=n;i++)>=n;i++)>=n;i++)>=n;i++)>=n;i++)>=n;i++)>=i;j++)>=i;j++)>=i-1;j++)>=i-1;j++)>=n;i++)>=n;i++)>=n;i++)>=n;i++)>=i-1;j++)>=i-1;j++)>=n;i++)>=n;i++)>