Table of Contents
Example
Algorithm
Output
Home Backend Development C++ Print symmetrical double triangle pattern in C language

Print symmetrical double triangle pattern in C language

Sep 14, 2023 pm 12:53 PM
c language Print double triangle

Print symmetrical double triangle pattern in C language

Given the number of rows, the program must print a symmetrical double triangle pattern with minimal complexity.

Example

Input: 5
Output:  X
          X
          O X
           O X
      X O X O X
       X O
        X O
         X
          X
Copy after login

The entire problem contains 3 different partitions −

  • For an odd number n , prints the n-1 lines of the upper half, and for an even number n, prints the n-2 lines of the upper half.

  • Print the middle line, for odd n, print 1 line, for even n, print 3 lines.

  • Print the lower half, for odd n, print n-1 lines, for even n, print n-2 lines.

Algorithm

START
   STEP 1: IF (n % 2 == 0) then
      x = x - 1;
      Define p as n – 1 for spaces
      Define s = 1 for characters
   STEP 2: LOOP FOR i= 1 AND i <= (x - 1) / 2 AND i++
      LOOP FOR j = 1 AND j <= p AND j++
         PRINT SPACE
      END LOOP
      IF i % 2 != 0 then
         GOTO STEP 11 Printx(s)
      ELSE
         GOTO STEP 12 Printo(s)
      END
      PRINT NEW LINE
      INCREMENT p BY 1
      LOOP FOR j = 1 AND j <= p AND j++
         PRINT SPACE
      END LOOP
      IF i % 2 != 0 then,
         GOTO STEP 11 Printx(s)
      ELSE
         GOTO STEP 12 Printo(s)
      END IF
         PRINT NEWLINE
         DECREMENT p BY 1
         INCREMENT s BY 1
      END LOOP
   STEP 3: IF n % 2 == 0 {
      LOOP FOR i = 1 AND i <= p AND i++
         PRINT SPACE
         IF n % 4 != 0 then,
            GOTO STEP 11 Printx(n/2)
         ELSE
            GOTO STEP 12 Printo(n/2)
         END IF
         PRINT SPACE
      END IF
   STEP 4: IF n % 2 != 0 then,
      GOTO STEP
      ELSE
         IF n % 4 != 0 then,
            DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2) and Printx(n/2)
         ELSE
            DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2) and Printo(n/2)
            DIVIDE n BY 2 AND GOTO STEP
         END IF
      END IF
      PRINT NEW LINE
   STEP 5: IF n % 2 == 0 then, {
      PRINT SPACE
      DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2)
      PRINT NEWLINE
      END IF
   STEP 6: SET p = 1
   STEP 7: IF n % 2 == 0
      DECREMENT x BY 1
      SET p = 2
      END IF
   STEP 8: SET q = x / 2
   STEP 9: LOOP FOR i = 1 AND i <= x AND i++) {
      LOOP FOR j = 1 AND j <= p AND j++
         PRINT SPACE
      END FOR
      PASS q TO STEP 11 Printx(q)
      IF i % 2 == 0 THEN
         DECREMENT q BY 1
      END IF
      PRINT NEWLINE
      INCREMENT p BY 1
   END FOR
   STEP 10: PRINT NEWLINE
   STEP 11: Printx(n)
      LOOP FOR i = 1 AND i< = n AND i++
         IF i % 2 != 0 then,
            PRINT x
         ELSE
            PRINT o
      END IF
   END FOR
   STEP 12: Printo(n)
      LOOP FOR i = 1 AND i< = n AND i++
      IF i % 2 != 0 then,
         PRINT o
      ELSE
      PRINT x
STOP
Copy after login

Example

#include <stdio.h>
// printing alternate x o starting with x
int printx(int n) {
   int i;
   for ( i = 1; i < = n; i++) {
      if (i % 2! = 0)
         printf("x ");
      else
         printf("o ");
   }
   return 0;
}
// printing alternate x o starting with o
int printo(int n) {
   int i;
   for ( i = 1; i < = n; i++) {
      if (i % 2! = 0)
         printf("o ");
      else
         printf("x ");
   }
   return 0;
}
// To print the pattern for n
int printpattern(int n) {
   // upper half
   // n-1 lines for odd, n-2 lines for even
   int x = n;
   int i, j;
   if (n % 2 == 0)
   x = x - 1;
   // number of spaces to leave in each line
   int p = n - 1;
   // number of characters in each line
   int s = 1;
   // prints double lines in each iteration
   for ( i = 1; i < = (x - 1) / 2; i++) {
      for ( j = 1; j < = p; j++) {
         printf(" ");
      }
      if (i % 2! = 0)
         printx(s);
      else
         printo(s);
         printf("</p><p>");
         p++;
      for ( j = 1; j < = p; j++)
         printf(" ");
      if (i % 2! = 0)
         printx(s);
      else
         printo(s);
         printf("</p><p>");
         p--;
         s++;
   }
   // extra upper middle for even
   if (n % 2 == 0) {
      for ( i = 1; i < = p; i++)
         printf(" ");
      if (n % 4! = 0)
         printx(n / 2);
      else
         printo(n / 2);
         printf("</p><p>");
   }
   // middle line
   if (n % 2! = 0)
      printx(n);
   else {
      if (n % 4! = 0) {
         printx(n / 2);
         printx(n / 2);
      } else {
         printx(n / 2);
         printo(n / 2);
      }
   }
   printf("</p><p>");
   // extra lower middle for even
   if (n % 2 == 0) {
      printf(" ");
      printx(n / 2);
      printf("</p><p>");
   }
   // lower half
   p = 1;
   if (n % 2 == 0) {
      x--;
      p = 2;
   }
   int q = x / 2;
   // one line for each iteration
   for ( i = 1; i < = x; i++) {
      for (int j = 1; j < = p; j++)
         printf(" ");
         printx(q);
      if (i % 2 == 0)
         q--;
         printf("</p><p>");
         p++;
   }
   printf("</p><p>");
   return 1;
}
int main() {
   int n = 5;
   printpattern(n);
   return 0;
}
Copy after login

Output

If we run the above program, it will generate the following output.

      X
       X
       O X
        O X
   X O X O X
    X O
     X O
      X
       X
Copy after login

The above is the detailed content of Print symmetrical double triangle pattern in C language. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

C language data structure: data representation and operation of trees and graphs C language data structure: data representation and operation of trees and graphs Apr 04, 2025 am 11:18 AM

C language data structure: The data representation of the tree and graph is a hierarchical data structure consisting of nodes. Each node contains a data element and a pointer to its child nodes. The binary tree is a special type of tree. Each node has at most two child nodes. The data represents structTreeNode{intdata;structTreeNode*left;structTreeNode*right;}; Operation creates a tree traversal tree (predecision, in-order, and later order) search tree insertion node deletes node graph is a collection of data structures, where elements are vertices, and they can be connected together through edges with right or unrighted data representing neighbors.

The truth behind the C language file operation problem The truth behind the C language file operation problem Apr 04, 2025 am 11:24 AM

The truth about file operation problems: file opening failed: insufficient permissions, wrong paths, and file occupied. Data writing failed: the buffer is full, the file is not writable, and the disk space is insufficient. Other FAQs: slow file traversal, incorrect text file encoding, and binary file reading errors.

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values ​​to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

How to output a countdown in C language How to output a countdown in C language Apr 04, 2025 am 08:54 AM

How to output a countdown in C? Answer: Use loop statements. Steps: 1. Define the variable n and store the countdown number to output; 2. Use the while loop to continuously print n until n is less than 1; 3. In the loop body, print out the value of n; 4. At the end of the loop, subtract n by 1 to output the next smaller reciprocal.

C language multithreaded programming: a beginner's guide and troubleshooting C language multithreaded programming: a beginner's guide and troubleshooting Apr 04, 2025 am 10:15 AM

C language multithreading programming guide: Creating threads: Use the pthread_create() function to specify thread ID, properties, and thread functions. Thread synchronization: Prevent data competition through mutexes, semaphores, and conditional variables. Practical case: Use multi-threading to calculate the Fibonacci number, assign tasks to multiple threads and synchronize the results. Troubleshooting: Solve problems such as program crashes, thread stop responses, and performance bottlenecks.

CS-Week 3 CS-Week 3 Apr 04, 2025 am 06:06 AM

Algorithms are the set of instructions to solve problems, and their execution speed and memory usage vary. In programming, many algorithms are based on data search and sorting. This article will introduce several data retrieval and sorting algorithms. Linear search assumes that there is an array [20,500,10,5,100,1,50] and needs to find the number 50. The linear search algorithm checks each element in the array one by one until the target value is found or the complete array is traversed. The algorithm flowchart is as follows: The pseudo-code for linear search is as follows: Check each element: If the target value is found: Return true Return false C language implementation: #include#includeintmain(void){i

Troubleshooting tips for processing files in C language Troubleshooting tips for processing files in C language Apr 04, 2025 am 11:15 AM

Troubleshooting Tips for C language processing files When processing files in C language, you may encounter various problems. The following are common problems and corresponding solutions: Problem 1: Cannot open the file code: FILE*fp=fopen("myfile.txt","r");if(fp==NULL){//File opening failed} Reason: File path error File does not exist without file read permission Solution: Check the file path to ensure that the file has check file permission problem 2: File reading failed code: charbuffer[100];size_tread_bytes=fread(buffer,1,siz

Concept of c language function Concept of c language function Apr 03, 2025 pm 10:09 PM

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

See all articles