先决条件: 循环 , If-Else语句 1.空心金字塔/三角形图案 这个图案类似于金字塔图案。唯一的区别是,我们将用空格字符替换所有内部“#”字符,并在最后一行中使用2*N-1(N=模式中的行数)“#”字符。 例如:
null
Input rows: 6Output: # # # # # # # # ## ############
C++
// CPP program to print a hollow pyramid pattern #include <iostream> using namespace std; void printPattern( int ); int main() { int n = 6; printPattern(n); } void printPattern( int n) { int i, j, k = 0; for (i = 1; i <= n; i++) // row=6 { // Print spaces for (j = i; j < n; j++) { cout << " " ; } // Print # while (k != (2 * i - 1)) { if (k == 0 || k == 2 * i - 2) cout << "#" ; else cout << " " ; k++; ; } k = 0; cout << endl; // print next row } // print last row for (i = 0; i < 2 * n - 1; i++) { cout << "#" ; } } |
JAVA
// JAVA program to print a hollow // pyramid pattern class GFG{ public static void main(String args[]) { int n = 6 ; printPattern(n); } static void printPattern( int n) { int i, j, k = 0 ; for (i = 1 ; i <= n; i++) // row=6 { // Print spaces for (j = i; j < n; j++) { System.out.print( " " ); } // Print # while (k != ( 2 * i - 1 )) { if (k == 0 || k == 2 * i - 2 ) System.out.print( "#" ); else System.out.print( " " ); k++; ; } k = 0 ; // print next row System.out.println(); } // print last row for (i = 0 ; i < 2 * n - 1 ; i++) { System.out.print( "#" ); } } } /*This code is contributed by Nikita Tiwari.*/ |
python
# Python program to print a hollow # pyramid pattern def printPattern( n) : k = 0 for i in range ( 1 ,n + 1 ) : #row 6 # Print spaces for j in range (i,n) : print ( ' ' , end = '') # Print # while (k ! = ( 2 * i - 1 )) : if (k = = 0 or k = = 2 * i - 2 ) : print ( '#' , end = '') else : print ( ' ' , end = '') k = k + 1 k = 0 ; print ("") # print next row # print last row for i in range ( 0 , 2 * n - 1 ) : print ( '#' , end = '') # Driver code n = 6 printPattern(n) # This code is contributed by Nikita Tiwari. |
PHP
<?php // php program to print a // hollow pyramid pattern function printPattern( $n ) { $k = 0; // row=6 for ( $i = 1; $i <= $n ; $i ++) { // Print spaces for ( $j = $i ; $j < $n ; $j ++) { echo " " ; } // Print # while ( $k != (2 * $i - 1)) { if ( $k == 0 || $k == 2 * $i - 2) echo "#" ; else echo " " ; $k ++; } $k = 0; // print next row echo "" ; } // print last row for ( $i = 0; $i < 2 * $n - 1; $i ++) { echo "#" ; } } //Driver Code $n = 6; printPattern( $n ); // This code is contributed by mits ?> |
Javascript
<script> // JavaScript program to print a hollow pyramid pattern var n = 6; printPattern(n); function printPattern(n) { var i, j, k = 0; for (i = 1; i <= n; i++) // row=6 { // Print spaces for (j = i; j < n; j++) { document.write( " " ); } // Print # while (k != 2 * i - 1) { if (k == 0 || k == 2 * i - 2) document.write( "#" ); else document.write( " " ); k++; } k = 0; document.write( "<br>" ); // print next row } // print last row for (i = 0; i < 2 * n - 1; i++) { document.write( "#" ); } } // This code is contributed by rdtank. </script> |
输出:
# # # # # # # # ## # # # # # # # # # # # #
本文由 希瓦尼·古泰尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END