0% found this document useful (0 votes)
3 views4 pages

Program No.6

The document contains a C program that generates various patterns based on user input for the number of rows. It includes a right-angle triangle with numbers, a diamond shape with numbers, a pyramid with asterisks, and a pyramid with alphabets. Each pattern is printed in a structured format using nested loops.

Uploaded by

raksheganesh50
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)
3 views4 pages

Program No.6

The document contains a C program that generates various patterns based on user input for the number of rows. It includes a right-angle triangle with numbers, a diamond shape with numbers, a pyramid with asterisks, and a pyramid with alphabets. Each pattern is printed in a structured format using nested loops.

Uploaded by

raksheganesh50
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

Program No.

6
Class : SE(E&TC), NBNSTIC,Pune

Title –

#include <stdio.h>

int main()
{
int i, j, k, n;

// 1. Right-angle triangle with numbers


printf("Enter the number of rows for patterns: ");
scanf("%d", &n);

printf("\n1. Right-angle triangle with numbers:\n");


for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d", j);
}
printf("\n");
}

// 2. Diamond shape with numbers


printf("\n2. Diamond shape with numbers:\n");
// Upper half
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
printf(" ");
for (j = 1; j <= i; j++)
printf("%d", i);
for (j = i - 1; j >= 1; j--)
printf("%d", i);
printf("\n");
}
// Lower half
for (i = n - 1; i >= 1; i--)
{
for (j = n; j > i; j--)
printf(" ");
for (j = 1; j <= i; j++)
printf("%d", i);
for (j = i - 1; j >= 1; j--)
printf("%d", i);
printf("\n");
}

// 3. Pyramid with asterisks


printf("\n3. Pyramid with asterisks:\n");
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
printf(" ");
for (j = 1; j <= (2 * i - 1); j++)
printf("*");
printf("\n");
}

// 4. Pyramid with alphabets


printf("\n4. Pyramid with alphabets:\n");
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
printf(" ");
char ch = 'A';
// Ascending part
for (j = 1; j <= i; j++)
{
printf("%c", ch++);
}
ch -= 2; // Step back one character
// Descending part
for (j = 1; j < i; j++)
{
printf("%c", ch--);
}
printf("\n");
}
return 0;
}

You might also like