Learn about Triangle Numbers Pattern Program in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Triangle Numbers Pattern Program in C
To print the below pattern in C programming then run the below code.
1 121 12321 1234321 123454321
Program:
#include<stdio.h>
int main()
{
int n, r, c, k, a;
printf("Enter number of rows: ");
scanf("%d",&n);
for(r=1;r<=n;r++)
{
for(c=1;c<=n-r;c++) printf(" ");
for(k=1;k<=(2*r-1);k++)
{
if(k<r) printf("%d",k);
else if(k==r)
{
printf("%d",k);
a=k;
}
else printf("%d",--a);
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows: 5 1 121 12321 1234321 123454321
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Pattern Programs 19 in C
Pattern Programs 18 in C