Q. Write a program to generate a following numbers structure:
(Where user entered number through keyboard, for example if num=5)
55555
44444
33333
22222
11111
Ans.
int num,r,c;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
for(r=num; r>=1; r--)
for(r=num; r>=1; r--)
{
for(c=num; c>=1; c--)
printf("%d",r);
printf("\n");
}
getch();
getch();
return 0;
}
/*************OUTPUT**************
Enter loop repeat number(rows): 5
***********************************/
/*************OUTPUT**************
Enter loop repeat number(rows): 5
55555
44444
33333
22222
11111
***********************************/
using single loop dis program is posible or not/
ReplyDelete@Gaurav Giri,
DeleteThis is not possible to single loop, because outer loop used for number of rows and inner loop used for number of columns repetition.
thanks alot sir but why to use post fix --,here both loops r increasing no?
DeleteYes, You can write your own code, also its depend on what we want to make design/pyramid.
DeleteWe can also make above pyramid program with differ code as:
#include"stdio.h"
int main()
{
int num=5,n,r,c;
for(r=1,n=num; r<=num; r++,n--)
{
for(c=1; c<=num; c++)
printf("%d",n);
printf("\n");
}
getch();
return 0;
}
/*output would be same as above program*/