热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

程序打印菱形

程序打印菱形原文:https://www . geesforgeks . org/c-program-print-diamond

程序打印菱形

原文:https://www . geesforgeks . org/c-program-print-diamond-shape/

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

diamond

C++


// C++ program to print diamond shape
// with 2n rows
#include
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
// 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("\n");
        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("\n");
        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("\n");
            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("\n");
            space++;
        }
    }
    // Driver Code
    public static void main(String[] args)
    {
        printDiamond(5);
    }
}
// This code is contributed
// by Arnav Kr. Mandal.


Python 3


# 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("\n");
            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("\n");
            space++;
        }
    }
    // Driver Code
    public static void Main()
    {
        printDiamond(5);
    }
}
// This code is contributed
// by Smitha Semwal.


服务器端编程语言(Professional Hypertext Preprocessor 的缩写)


<?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("\n");
        $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("\n");
        $space++;
    }
}
    // Driver code
    printDiamond(5);
// This code is contributed by Anuj_67
?>


java 描述语言


<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("
"
);
          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("
"
);
          space++;
        }
      }
      // Driver code
      printDiamond(5);
      // This code is contributed by rdtank.
    script>

输出:

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

本文由拉胡尔·辛格(尼特·KKR)供稿。如果你喜欢 GeeksforGeeks 并想投稿,你也可以使用contribute.geeksforgeeks.org写一篇文章或者把你的文章邮寄到 contribute@geeksforgeeks.org。看到你的文章出现在极客博客主页上,帮助其他极客。
如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。


推荐阅读
  • Java 中 Writer flush()方法,示例 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文介绍如何使用 Python 将一个字符串按照指定的行和元素分隔符进行两次拆分,最终将字符串转换为矩阵形式。通过两种不同的方法实现这一功能:一种是使用循环与 split() 方法,另一种是利用列表推导式。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文介绍了如何使用 Spring Boot DevTools 实现应用程序在开发过程中自动重启。这一特性显著提高了开发效率,特别是在集成开发环境(IDE)中工作时,能够提供快速的反馈循环。默认情况下,DevTools 会监控类路径上的文件变化,并根据需要触发应用重启。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
author-avatar
网络营销论坛闯天涯
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有