Table of Contents
示例
ALGORITHM
Example
输出
Home Backend Development C++ C program for hexagon pattern

C program for hexagon pattern

Sep 19, 2023 pm 03:25 PM
c program hexagon pattern

我们被给定一个整数'n',任务是生成六边形图案并显示最终输出。

示例

Input-: n=5
Output-:
Copy after login

C program for hexagon pattern

Input-: n = 4
Output-:
Copy after login

C program for hexagon pattern

Approach we are using in the given program is as follows

  • Input the number ‘n’ from user
  • Divide the entire pattern into three parts i.e. upper part, middle part and lower part Start loop i for printing the upper part of the pattern from i to 0 and i to be less than n and keep incrementing the value of i Start loop m for printing the middle part of the pattern from m to 0 and m to be less than n-2 and keep incrementing the value of m Start loop h for printing the lower part of the pattern from h to res and h to be greater than 0 and keep decrementing the value of h Keep printing the * with spaces.

ALGORITHM

START
Step 1-> declare function to print hexagonal pattern
   void pattern(int n)
   Declare and set int len = 2 * n - 1
   Loop For int i = 0 and i < n and i++
      declare and set int temp = i + n
      Loop For int k = 0 and k < temp and k++
         IF ((k == n + i - 1) || (k == n - i - 1))
            print *
         End
         Else
            print space
         End
            Print </p><p>
      End
      Loop For int m = 0 and m < n - 2 and m++
         Loop For int j = 0 and j < len and j++
            if (j == 0 || j == len - 1)
               Print *
            End
            Else
               print space
            End
         End
         Print </p><p>
      End
      declare and set int res = n - 1
      Loop For int h = res and h >= 0 and h--
         declare and set int temp2 = h + n
         Loop For int k = 0 and k < temp2 and k++
            if ((k == n + h - 1) || (k == n - h - 1))
               print *
            End
            Else
               print space
            End
         End
         Print </p><p>
      End
   End
Step 2-> In main()
   Declare variable int n = 5
   call pattern(n)
STOP
Copy after login

Example

的中文翻译为:

示例

#include <stdio.h>
//program to print hexagon pattern  
void pattern(int n) {
   int len = 2 * n - 1;
   //for loop for upper part of a pattern
   for (int i = 0; i < n; i++) {
      int temp = i + n;
      for (int k = 0; k < temp; k++) {
         if ((k == n + i - 1) || (k == n - i - 1))
            printf("*");
         else
            printf(" ");
      }
      printf("</p><p>");
   }
   //for loop for mid part of a pattern
   for (int m = 0; m < n - 2; m++) {
      for (int j = 0; j < len; j++) {
         if (j == 0 || j == len - 1)
            printf("*");
         else
            printf(" ");
      }
      printf("</p><p>");
   }
   //for loop for lower part of a pattern
   int res = n - 1;
   for (int h = res; h >= 0; h--) {
      int temp2 = h + n;
      for (int k = 0; k < temp2; k++) {
         if ((k == n + h - 1) || (k == n - h - 1))
            printf("*");
         else
            printf(" ");
      }
      printf("</p><p>");
   }
}
int main() {
   int n = 5;
   pattern(n);
   return 0;
}
Copy after login

输出

C program for hexagon pattern

The above is the detailed content of C program for hexagon pattern. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
Translate the following into Chinese: C program to convert Roman numerals to decimal numbers Translate the following into Chinese: C program to convert Roman numerals to decimal numbers Sep 05, 2023 pm 09:53 PM

Given below is a C language algorithm to convert Roman numerals to decimal numbers: Algorithm Step 1 - Start Step 2 - Read Roman numerals at runtime Step 3 - Length: = strlen(roman) Step 4 - For i=0 to Length-1 Step 4.1-switch(roman[i]) Step 4.1.1-case'm': &nbs

C++ program to compare the lexicographic order of two strings C++ program to compare the lexicographic order of two strings Sep 04, 2023 pm 05:13 PM

Lexicographic string comparison means that strings are compared in dictionary order. For example, if there are two strings 'apple' and 'appeal', the first string will come last because the first three characters of 'app' are the same. Then for the first string the character is 'l' and in the second string the fourth character is 'e'. Since 'e' is shorter than 'l', it will come first if we sort lexicographically. Strings are compared lexicographically before being arranged. In this article, we will see different techniques for lexicographically comparing two strings using C++. Using the compare() function in C++ strings The C++string object has a compare()

Specific steps to create a hexagonal cluster layout in PPT Specific steps to create a hexagonal cluster layout in PPT Mar 26, 2024 pm 03:21 PM

1. First start PPT2010, execute the insert-picture command, insert a landscape picture material, and adjust the size. 2. Execute the insert command again, select smartart and then bring up the select smartart graphics dialog box. 3. Switch to the Picture tab, select the hexagonal cluster, and click the OK button to view the effect. 4. Then execute the Format-Add Shape command and click multiple times to add. Of course, adding shapes should be based on your actual situation. 5. Select the hexagon cluster and execute the Design-Convert-Convert to Shape command to convert it into a graphic. 6. Select the image to execute the Cut command, right-click the shape and select the Format Shape command from the drop-down menu. 7. In the Format Shape dialog box, set the fill to a picture or texture.

C program to find length of linked list C program to find length of linked list Sep 07, 2023 pm 07:33 PM

Linked lists use dynamic memory allocation, i.e. they grow and shrink accordingly. They are defined as collections of nodes. Here, a node has two parts, data and links. The representation of data, links and linked lists is as follows - Types of linked lists There are four types of linked lists, as follows: - Single linked list/Singly linked list Double/Doubly linked list Circular single linked list Circular double linked list We use the recursive method to find the length of the linked list The logic is -intlength(node *temp){ if(temp==NULL) returnl; else{&n

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument Sep 17, 2023 am 10:49 AM

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

C program uses rename() function to change file name C program uses rename() function to change file name Sep 21, 2023 pm 10:01 PM

The rename function changes a file or directory from its old name to its new name. This operation is similar to the move operation. So we can also use this rename function to move files. This function exists in the stdio.h library header file. The syntax of the rename function is as follows: intrename(constchar*oldname,constchar*newname); The function of the rename() function accepts two parameters. One is oldname and the other is newname. Both parameters are pointers to constant characters that define the old and new names of the file. Returns zero if the file was renamed successfully; otherwise, returns a nonzero integer. During a rename operation

C++ program to get the imaginary part of a given complex number C++ program to get the imaginary part of a given complex number Sep 06, 2023 pm 06:05 PM

Modern science relies heavily on the concept of plural numbers, which was first established in the early 17th century by Girolamo Cardano, who introduced it in the 16th century. The formula for complex numbers is a+ib, where a holds the html code and b is a real number. A complex number is said to have two parts: the real part <a> and the imaginary part (<ib>). The value of i or iota is √-1. The plural class in C++ is a class used to represent complex numbers. The complex class in C++ can represent and control several complex number operations. Let's take a look at how to represent and control the display of plural numbers. imag() member function As mentioned above, complex numbers are composed of real part and imaginary part. To display the real part we use real()

C++ program to print dictionary C++ program to print dictionary Sep 11, 2023 am 10:33 AM

A map is a special type of container in C++ in which each element is a pair of two values, namely a key value and a map value. The key value is used to index each item, and the mapped value is the value associated with the key. Regardless of whether the mapped value is unique, the key is always unique. To print map elements in C++ we have to use iterator. An element in a set of items is indicated by an iterator object. Iterators are primarily used with arrays and other types of containers (such as vectors), and they have a specific set of operations that can be used to identify specific elements within a specific range. Iterators can be incremented or decremented to reference different elements present in a range or container. The iterator points to the memory location of a specific element in the range. Printing a map in C++ using iterators First, let's look at how to define

See all articles