Q. Write a C program to print the following half-square number triangle C program as :
543212345
4321234
32123
212
1
Ans.
/*c program for half-square number triangle*/
#include<stdio.h>
int main()
{
int num,r,c,n,sp,p;
printf("Enter any number : ");
scanf("%d", &num);
for(r=1,n=num; r<=num; r++,n--)
{
for(sp=r; sp>1; sp--)
printf(" ");
for(c=r,p=n; c<=num; c++,p--)
printf("%d",p);
for(c=num-r,p=2; c>=1; c--,p++)
printf("%d",p);
printf("\n");
}
getch();
return 0;
}
The output of above program would be:
543212345
4321234
32123
212
1
Ans.
/*c program for half-square number triangle*/
#include<stdio.h>
int main()
{
int num,r,c,n,sp,p;
printf("Enter any number : ");
scanf("%d", &num);
for(r=1,n=num; r<=num; r++,n--)
{
for(sp=r; sp>1; sp--)
printf(" ");
for(c=r,p=n; c<=num; c++,p--)
printf("%d",p);
for(c=num-r,p=2; c>=1; c--,p++)
printf("%d",p);
printf("\n");
}
getch();
return 0;
}
The output of above program would be:
Figure : Screen shot for Half-Square Number Triangle C program |
Comments
Post a Comment