Q. Write a C program to make the following perfect equal number triangle shape as:
4
4 3 4
4 3 2 3 4
4 3 2 1 2 3 4
4 3 2 3 4
4 3 4
4
Ans.
Its looks very tough to make the above number triangle but when you divided this triangle (pyramid) in sub-triangle (sub-pyramid) then its look like as:
4
4 3 4
4 3 2 3 4
4 3 2 1 2 3 4
4 3 2 3 4
4 3 4
4
Now you can see, we divided above pyramid in 4 sub-pyramid, so it is easy to make 4 small pyramid program and marge them, and your pyramid design program completed. cheers!!
/*c program for Perfect Equal Number Triangle*/
#include<stdio.h>
int main()
{
int num=4,n,r,c,p,x,y,z;
y=num+1;
for(r=1; r<=num; r++,y--)
{
for(c=1,n=num; c<=r; c++,n--)
printf(" %d",n);
for(c=1,p=y; c<r; c++,p++)
printf(" %d",p);
printf("\n");
}
for(r=num-1,x=1; r>=1; r--,x--)
{
for(c=r,n=num; c>=1; c--,n--)
printf(" %d",n);
for(c=r,z=num-x; c>1; c--,z++)
printf(" %d",z);
printf("\n");
}
getch();
return 0;
}
You might also like to read:
4
4 3 4
4 3 2 3 4
4 3 2 1 2 3 4
4 3 2 3 4
4 3 4
4
Ans.
Its looks very tough to make the above number triangle but when you divided this triangle (pyramid) in sub-triangle (sub-pyramid) then its look like as:
4
4 3 4
4 3 2 3 4
4 3 2 1 2 3 4
4 3 2 3 4
4 3 4
4
Now you can see, we divided above pyramid in 4 sub-pyramid, so it is easy to make 4 small pyramid program and marge them, and your pyramid design program completed. cheers!!
/*c program for Perfect Equal Number Triangle*/
#include<stdio.h>
int main()
{
int num=4,n,r,c,p,x,y,z;
y=num+1;
for(r=1; r<=num; r++,y--)
{
for(c=1,n=num; c<=r; c++,n--)
printf(" %d",n);
for(c=1,p=y; c<r; c++,p++)
printf(" %d",p);
printf("\n");
}
for(r=num-1,x=1; r>=1; r--,x--)
{
for(c=r,n=num; c>=1; c--,n--)
printf(" %d",n);
for(c=r,z=num-x; c>1; c--,z++)
printf(" %d",z);
printf("\n");
}
getch();
return 0;
}
/*****************************************
The output of above number triangle C program would be:
******************************************/
Figure: Screen-shot for Perfect Equal Number Triangle C Program |
You might also like to read:
we can also write code like below of above example.
ReplyDelete#include
#include
int main()
{
int i,j,k,l,m;
for(i=1;i<=4;i++)
{
l=4;
for(k=1;k<=2*i-1;k++)
{
if(k>=i)
{
printf("%d ",l++);
}
else
printf("%d ",l--);
}
printf("\n");
}
for(i=3;i>=1;i--)
{
l=4;
for(k=1;k<=2*i-1;k++)
{
if(k>=i)
{
printf("%d ",l++);
}
else
printf("%d ",l--);
}
printf("\n");
}
}