Note - Double Click to Copy Code Contact Us!

Patterns using C Programming

Tech Doubility

 There are many different patterns that can be printed using C programming. Here are a few examples:



1. Right-angle triangle

C
#include <stdio.h>

int main() {
  int n;
  printf("Enter the number of rows: ");
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      printf("* ");
    }
    printf("\n");
  }

  return 0;
}

Output:

Code snippet
Enter the number of rows: 5

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

2. Inverted right-angle triangle

C
#include <stdio.h>

int main() {
  int n;
  printf("Enter the number of rows: ");
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - i - 1; j++) {
      printf(" ");
    }
    for (int j = 0; j <= i; j++) {
      printf("* ");
    }
    printf("\n");
  }

  return 0;
}

Output:

Code snippet
Enter the number of rows: 5

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

3. Arrow-shaped pattern

C
#include <stdio.h>

int main() {
  int n;
  printf("Enter the number of rows: ");
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - i - 1; j++) {
      printf(" ");
    }
    for (int j = 0; j <= i; j++) {
      printf("* ");
    }
    printf("\n");
  }

  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < n - i - 1; j++) {
      printf(" ");
    }
    for (int j = 0; j <= i; j++) {
      printf("* ");
    }
    printf("\n");
  }

  return 0;
}

Output:

Code snippet
Enter the number of rows: 5

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

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

These are just a few examples of the many patterns that can be printed using C programming. You can experiment with different patterns and see what you can create.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.