Home > Backend Development > C++ > C program arrow star pattern

C program arrow star pattern

PHPz
Release: 2023-08-25 17:09:05
forward
1387 people have browsed it

Given a number n and we have to print the arrow star pattern of maximum n number of stars.

The star pattern of input 4 will look like −

C program arrow star pattern

Example

Input: 3
Output:
Copy after login

C program arrow star pattern

Input: 5
Output:
Copy after login

C program arrow star pattern

The method used below is as follows:

  • Enter in integer form .
  • Then print n spaces and n asterisks.
  • Decrease until n>1.
  • Now increment until n.
  • And print spaces and asterisks in increasing order.

Algorithm

Start
In function int arrow(int num)
   Step 1-> declare and initialize i, j
   Step 2-> Loop For i = 1 and i <= num and i++
      Loop For j = i and j < num and j++
         Print a space
      Loop For j = i and j <= num and j++
         Print "*"
         Print newline
   Step 3-> Loop For i = 2 and i <= num and i++
      Loop For j= 1 and j < I and j++
         Print a space
      Loop For j = 1 and j <= i and j++
         Print "*"
         Print newline
In function int main()
   Step 1-> declare and initialize num = 4
   Step 2-> call arrow(num)
Copy after login

Example

Demonstration

#include <stdio.h>
// arrow function
int arrow(int num) {
   int i, j;
   // Prints the upper part of the arrow
   for (i = 1; i <= num; i++) {
      // to print the spaces
      for (j = i; j < num; j++) {
         printf(" ");
      }
      // to print the * for the pattern
      for (j = i; j <= num; j++) {
         printf("*");
      }
      printf("</p><p>");
   }
   // Prints lower part of the arrow
   for (i = 2; i <= num; i++) {
      // to print the spaces
      for (j = 1; j < i; j++) {
         printf(" ");
      }
      // to print the * for the pattern
      for (j = 1; j <= i; j++) {
         printf("*");
      }
      printf("</p><p>");
   }
   return 0;
}
int main() {
   // get the value from user
   int num = 4;
   // function calling
   arrow(num);
   return 0;
}
Copy after login

Output

If you run the above code, the following output will be generated −

C program arrow star pattern

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

Related labels:
source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template