热门标签 | 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。看到你的文章出现在极客博客主页上,帮助其他极客。
如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。


推荐阅读
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 将Web服务部署到Tomcat
    本文介绍了如何在JDeveloper 12c中创建一个Java项目,并将其打包为Web服务,然后部署到Tomcat服务器。内容涵盖从项目创建、编写Web服务代码、配置相关XML文件到最终的本地部署和验证。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • 本文介绍如何使用 NSTimer 实现倒计时功能,详细讲解了初始化方法、参数配置以及具体实现步骤。通过示例代码展示如何创建和管理定时器,确保在指定时间间隔内执行特定任务。 ... [详细]
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社区 版权所有