Q. Write a C program to print the following number triangle or number structure:
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
Ans.
/*c program for above number triangle codes*/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,c,sp,num;
printf("Enter loop repeat number(rows): ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(sp=1; sp<=num-r; sp++)
printf(" "); //it is 2 blank space
for(c=1; c<=2*r-1; c++)
printf(" %d",c);
printf("\n");
}
getch();
return 0;
}
/**************Output**************/
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
Ans.
/*c program for above number triangle codes*/
#include<stdio.h>
#include<conio.h>
int main()
{
int r,c,sp,num;
printf("Enter loop repeat number(rows): ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(sp=1; sp<=num-r; sp++)
printf(" "); //it is 2 blank space
for(c=1; c<=2*r-1; c++)
printf(" %d",c);
printf("\n");
}
getch();
return 0;
}
/**************Output**************/
Screen shot for number triangle C program |
No comments:
Post a Comment