Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)
1 | 2 | 3 | 4 | 5 | 4 | 3 | 2 | 1 |
2 | 3 | 4 | 5 | 4 | 3 | 2 | ||
3 | 4 | 5 | 4 | 3 | ||||
4 | 5 | 4 | ||||||
5 |
Ans.
/* c program for number structure */
#include<stdio.h>
#include<conio.h>
int main()
{
int num,r,c,sp;
printf("Enter loop repeat number : ");
scanf("%d",&num);
for(r=1; r<=num; r++)
{
for(sp=r; sp>1; sp--)
printf(" ");
for(c=r; c<=num; c++)
printf("%d",c);
for(c=num-1; c>=r; c--)
printf("%d",c);
printf("\n");
}
getch():
return 0;
}
/************** OUTPUT *************
Enter loop repeat number : 5
***********************************/
/* c program for number structure */
#include<stdio.h>
#include<conio.h>
int main()
{
int num,r,c,sp;
printf("Enter loop repeat number : ");
scanf("%d",&num);
for(r=1; r<=num; r++)
{
for(sp=r; sp>1; sp--)
printf(" ");
for(c=r; c<=num; c++)
printf("%d",c);
for(c=num-1; c>=r; c--)
printf("%d",c);
printf("\n");
}
getch():
return 0;
}
/************** OUTPUT *************
Enter loop repeat number : 5
1 | 2 | 3 | 4 | 5 | 4 | 3 | 2 | 1 |
2 | 3 | 4 | 5 | 4 | 3 | 2 | ||
3 | 4 | 5 | 4 | 3 | ||||
4 | 5 | 4 | ||||||
5 |
No comments:
Post a Comment