0% found this document useful (0 votes)
35 views6 pages

Star Patterns in C

Uploaded by

RAKSHITH REDDY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

Star Patterns in C

Uploaded by

RAKSHITH REDDY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Star Patterns in C

print all star based patterns using c language


take input from user

Here's a C program to print various star-based patterns based on user input:

#include <stdio.h>

void printPatterns(int n) {
printf("Pattern 1 (Right Triangle):\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}

printf("\nPattern 2 (Inverted Right Triangle):\n");


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

printf("\nPattern 3 (Pyramid):\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}

printf("\nPattern 4 (Inverted Pyramid):\n");


for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {

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

printf("\nPattern 5 (Diamond):\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
}

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

printPatterns(n);

return 0;
}

Explanation:
1. Pattern 1 (Right Triangle): Prints a right-angled triangle of stars.

2/6
2. Pattern 2 (Inverted Right Triangle): Prints an inverted right-angled triangle of stars.

3. Pattern 3 (Pyramid): Prints a pyramid shape of stars.

4. Pattern 4 (Inverted Pyramid): Prints an inverted pyramid shape of stars.

5. Pattern 5 (Diamond): Prints a diamond-shaped pattern of stars.

Input:
The user is asked to enter the number of rows, and the program prints all the above patterns
accordingly.

generate codes separately

Here are the separate C programs for each star-based pattern:

1. Right Triangle Pattern

#include <stdio.h>

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

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

printRightTriangle(n);

return 0;
}

2. Inverted Right Triangle Pattern

3/6
c

#include <stdio.h>

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

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

printInvertedRightTriangle(n);

return 0;
}

3. Pyramid Pattern

#include <stdio.h>

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

int main() {
int n;

4/6
printf("Enter the number of rows: ");
scanf("%d", &n);

printPyramid(n);

return 0;
}

4. Inverted Pyramid Pattern

#include <stdio.h>

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

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

printInvertedPyramid(n);

return 0;
}

5. Diamond Pattern

#include <stdio.h>

5/6
void printDiamond(int n) {
// Top half of the diamond
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}

// Bottom half of the diamond


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

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

printDiamond(n);

return 0;
}

Each of these C programs generates a specific star-based pattern based on the number of
rows entered by the user.

6/6

You might also like