打印钻石形状的程序

给定一个数字n,编写一个程序来打印一个2n行的菱形。 例如:

null

diamond

C++

// C++ program to print diamond shape
// with 2n rows
#include <bits/stdc++.h>
using namespace std;
// Prints diamond pattern with 2n rows
void printDiamond( int n)
{
int space = n - 1;
// run loop (parent loop)
// till number of rows
for ( int i = 0; i < n; i++)
{
// loop for initially space,
// before star printing
for ( int j = 0;j < space; j++)
cout << " " ;
// Print i+1 stars
for ( int j = 0; j <= i; j++)
cout << "* " ;
cout << endl;
space--;
}
// Repeat again in reverse order
space = 0;
// run loop (parent loop)
// till number of rows
for ( int i = n; i > 0; i--)
{
// loop for initially space,
// before star printing
for ( int j = 0; j < space; j++)
cout << " " ;
// Print i stars
for ( int j = 0;j < i;j++)
cout << "* " ;
cout << endl;
space++;
}
}
// Driver code
int main()
{
printDiamond(5);
return 0;
}
// This is code is contributed
// by rathbhupendra


C

// C program to print
// diamond shape with
// 2n rows
#include<stdio.h>
// Prints diamond
// pattern with 2n rows
void printDiamond( int n)
{
int space = n - 1;
// run loop (parent loop)
// till number of rows
for ( int i = 0; i < n; i++)
{
// loop for initially space,
// before star printing
for ( int j = 0;j < space; j++)
printf ( " " );
// Print i+1 stars
for ( int j = 0;j <= i; j++)
printf ( "* " );
printf ( "" );
space--;
}
// Repeat again in
// reverse order
space = 0;
// run loop (parent loop)
// till number of rows
for ( int i = n; i > 0; i--)
{
// loop for initially space,
// before star printing
for ( int j = 0; j < space; j++)
printf ( " " );
// Print i stars
for ( int j = 0;j < i;j++)
printf ( "* " );
printf ( "" );
space++;
}
}
// Driver code
int main()
{
printDiamond(5);
return 0;
}


JAVA

// JAVA Code to print
// the diamond shape
import java.util.*;
class GFG
{
// Prints diamond pattern
// with 2n rows
static void printDiamond( int n)
{
int space = n - 1 ;
// run loop (parent loop)
// till number of rows
for ( int i = 0 ; i < n; i++)
{
// loop for initially space,
// before star printing
for ( int j = 0 ; j < space; j++)
System.out.print( " " );
// Print i+1 stars
for ( int j = 0 ; j <= i; j++)
System.out.print( "* " );
System.out.print( "" );
space--;
}
// Repeat again in
// reverse order
space = 0 ;
// run loop (parent loop)
// till number of rows
for ( int i = n; i > 0 ; i--)
{
// loop for initially space,
// before star printing
for ( int j = 0 ; j < space; j++)
System.out.print( " " );
// Print i stars
for ( int j = 0 ; j < i; j++)
System.out.print( "* " );
System.out.print( "" );
space++;
}
}
// Driver Code
public static void main(String[] args)
{
printDiamond( 5 );
}
}
// This code is contributed
// by Arnav Kr. Mandal.


Python3

# Python program to
# print Diamond shape
# Function to print
# Diamond shape
def Diamond(rows):
n = 0
for i in range ( 1 , rows + 1 ):
# loop to print spaces
for j in range ( 1 , (rows - i) + 1 ):
print (end = " " )
# loop to print star
while n ! = ( 2 * i - 1 ):
print ( "*" , end = "")
n = n + 1
n = 0
# line break
print ()
k = 1
n = 1
for i in range ( 1 , rows):
# loop to print spaces
for j in range ( 1 , k + 1 ):
print (end = " " )
k = k + 1
# loop to print star
while n < = ( 2 * (rows - i) - 1 ):
print ( "*" , end = "")
n = n + 1
n = 1
print ()
# Driver Code
# number of rows input
rows = 5
Diamond(rows)


C#

// C# Code to print
// the diamond shape
using System;
class GFG
{
// Prints diamond pattern
// with 2n rows
static void printDiamond( int n)
{
int space = n - 1;
// run loop (parent loop)
// till number of rows
for ( int i = 0; i < n; i++)
{
// loop for initially space,
// before star printing
for ( int j = 0; j < space; j++)
Console.Write( " " );
// Print i+1 stars
for ( int j = 0; j <= i; j++)
Console.Write( "* " );
Console.Write( "" );
space--;
}
// Repeat again in
// reverse order
space = 0;
// run loop (parent loop)
// till number of rows
for ( int i = n; i > 0; i--)
{
// loop for initially space,
// before star printing
for ( int j = 0; j < space; j++)
Console.Write( " " );
// Print i stars
for ( int j = 0; j < i; j++)
Console.Write( "* " );
Console.Write( "" );
space++;
}
}
// Driver Code
public static void Main()
{
printDiamond(5);
}
}
// This code is contributed
// by Smitha Semwal.


PHP

<?php
// PHP program to print
// diamond shape with
// 2n rows
// Prints diamond $
// pattern with 2n rows
function printDiamond( $n )
{
$space = $n - 1;
// run loop (parent loop)
// till number of rows
for ( $i = 0; $i < $n ; $i ++)
{
// loop for initially space,
// before star printing
for ( $j = 0; $j < $space ; $j ++)
printf( " " );
// Print i+1 stars
for ( $j = 0; $j <= $i ; $j ++)
printf( "* " );
printf( "" );
$space --;
}
// Repeat again in
// reverse order
$space = 0;
// run loop (parent loop)
// till number of rows
for ( $i = $n ; $i > 0; $i --)
{
// loop for initially space,
// before star printing
for ( $j = 0; $j < $space ; $j ++)
printf( " " );
// Pr$i stars
for ( $j = 0; $j < $i ; $j ++)
printf( "* " );
printf( "" );
$space ++;
}
}
// Driver code
printDiamond(5);
// This code is contributed by Anuj_67
?>


Javascript

<script>
// JavaScript program to print diamond shape
// with 2n rows
// Prints diamond pattern with 2n rows
function printDiamond(n) {
var space = n - 1;
// run loop (parent loop)
// till number of rows
for ( var i = 0; i < n; i++) {
// loop for initially space,
// before star printing
for ( var j = 0; j < space; j++) document.write( "  " );
// Print i+1 stars
for ( var j = 0; j <= i; j++) document.write( "*" + "  " );
document.write( "<br>" );
space--;
}
// Repeat again in reverse order
space = 0;
// run loop (parent loop)
// till number of rows
for ( var i = n; i > 0; i--)
{
// loop for initially space,
// before star printing
for ( var j = 0; j < space; j++) document.write( "  " );
// Print i stars
for ( var j = 0; j < i; j++) document.write( "*" + "  " );
document.write( "<br>" );
space++;
}
}
// Driver code
printDiamond(5);
// This code is contributed by rdtank.
</script>


输出:

        *       * *      * * *     * * * *    * * * * *    * * * * *     * * * *      * * *       * *        *

本文由 拉胡尔·辛格(Nit KKR) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享