Right Triangle and Its Mirror
#include<stdio.h>
// Left-aligned triangle
void tr(int r){
int i, j;
for(i = 0; i <= r; i++, printf("\n"))
for(j = 0; j <= i; j++)
printf("*");
}
// Right-aligned triangle
void rtr(int r){
int i, j;
for(i = 0; i <= r; i++, printf("\n"))
for(j = 0; j <= r; j++)
printf(j < r - i ? " " : "*");
}
// Inverted left-aligned triangle
void itr(int r){
int i, j;
for(i = r; i >= 0; i--, printf("\n")) // Corrected the loop condition
for(j = 0; j <= i; j++)
printf("*");
}
// Full pyramid
void pr(int r){
int i, j;
for(i = 0; i <= r; i++, printf("\n")) {
for(j = 0; j <= r - i; j++) // spaces
printf(" ");
for(j = 0; j < 2*i - 1; j++) // stars
printf("*");
}
}
// Inverted Pyramid
void ipr(int r){
int i, j;
for(i = r; i >= 0; i--, printf("\n")) {
for(j = 0; j <= r - i; j++) // spaces
printf(" ");
for(j = 0; j < 2*i - 1; j++) // stars
printf("*");
}
}
// Inverted Mirrored Right Triangle
void ritr(int r){
int i, j;
for(i = r; i >= 0; i--, printf("\n"))
for(j = 0; j <= r; j++)
printf(j < r - i ? " " : "*");
}
int main(){
tr(5); // Left-aligned triangle
printf("\n"); // Separator
rtr(5); // Right-aligned triangle
printf("\n"); // Separator
itr(5); // Inverted left-aligned triangle
printf("\n"); // Separator
pr(5); // Full pyramid
printf("\n"); // Separator
ipr(5); // Inverted pyramid
printf("\n"); // Separator
ritr(5); // Inverted mirrored right triangle
return 0;
}
output:
*
**
***
****
*****
******
*
**
***
****
*****
******
******
*****
****
***
**
*
*
***
*****
*******
*********
*********
*******
*****
***
*
******
*****
****
***
**
*
Number Triangle
#include<stdio.h>
// Left-aligned triangle with repeated numbers
void tr1(int r){
int i, j;
for(i = 1; i <= r; i++, printf("\n"))
for(j = 1; j <= i; j++)
printf("%d",i); // Print the row number i
}
// Left-aligned triangle with increasing numbers
void tr2(int r){
int i, j;
for(i = 1; i <= r; i++, printf("\n"))
for(j = 1; j <= i; j++)
printf("%d",j); // Print numbers from 1 to i
}
// Palindromic number triangle
void palin(int r){
int i, j;
for(i = 1; i <= r; i++, printf("\n")) {
// Print leading spaces for centering
for(j = 1; j <= r - i; j++)
printf(" ");
// Print ascending part of the palindrome
for(j = 1; j <= i; j++)
printf("%d",j);
// Print descending part of the palindrome
for(j = i - 1; j >= 1; j--)
printf("%d",j);
}
}
// Centered triangle with increasing numbers
void center(int r){
int i, j;
for(i = 1; i <= r; i++, printf("\n")) {
// Print leading spaces for centering
for(j = 1; j <= r - i; j++)
printf(" ");
// Print numbers increasing from 1 to i
for(j = 1; j <= i; j++)
printf("%d", j);
// Print numbers decreasing from i-1 to 1
for(j = i - 1; j >= 1; j--)
printf("%d", j);
}
}
// Inverted Palindromic number triangle
void ipalin(int r){
int i, j;
for(i = r; i >= 1; i--, printf("\n")) {
// Print leading spaces for centering
for(j = 1; j <= r - i; j++)
printf(" ");
// Print ascending part of the palindrome
for(j = 1; j <= i; j++)
printf("%d",j);
// Print descending part of the palindrome
for(j = i - 1; j >= 1; j--)
printf("%d",j);
}
}
// Inverted Centered triangle with decreasing numbers
void icenter(int r){
int i, j;
for(i = r; i >= 1; i--, printf("\n")) {
// Print leading spaces for centering
for(j = 1; j <= r - i; j++)
printf(" ");
// Print numbers increasing from 1 to i
for(j = 1; j <= i; j++)
printf("%d", j);
// Print numbers decreasing from i-1 to 1
for(j = i - 1; j >= 1; j--)
printf("%d", j);
}
}
int main(){
tr1(5); // Left-aligned triangle with repeated numbers
printf("\n"); // Separator
tr2(5); // Left-aligned triangle with increasing numbers
printf("\n"); // Separator
palin(5); // Palindromic number triangle
printf("\n"); // Separator
center(5); // Centered triangle with increasing numbers
printf("\n"); // Separator
ipalin(5); // Inverted Palindromic number triangle
printf("\n"); // Separator
icenter(5); // Inverted Centered triangle with decreasing numbers
return 0;
}
1
22
333
4444
55555
1
12
123
1234
12345
1
121
12321
1234321
123454321
1
121
12321
1234321
123454321
123454321
1234321
12321
121
1
123454321
1234321
12321
121
1
0 Comments had been done yet:
Post a Comment