Q. Write a program to generate a following numbers structure:
(Where user entered number through keyboard, for example if num=5)
11111
22222
33333
44444
55555
Ans.
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
for(r=1; num>=r; r++)
for(r=1; num>=r; r++)
{
for(c=num; c>=1; c--)
printf("%d",r);
printf("\n");
}
return 0;
}
/*************OUTPUT****************
Enter loop repeat number(rows): 5
************************************/
/*************OUTPUT****************
Enter loop repeat number(rows): 5
11111
22222
33333
44444
55555
************************************/
sir i cant understand why for(c=num;c>=1;c--)
ReplyDeleteplz reply today
@pritesh,
Deletefor(c=num;c>=1;c--)
we use c=num bcoz we want to repeat room equal to num every times. So if assume num=5 then, c=num(i.e. c=5)
Now checking condition c>=1(i.e. 5>=1)
condition true, so compiler goes into loop and execute statement in it.
When all statement are executed then compiler goes to third part of for loop(i.e. increment/decrement) and in our program it is decrement (i.e. c--) so now c value is 4.
Now second times condition check 4>=1,
condition true,
compiler goes into loop and statement executed,
When its over, compiler goes to increment/decrement part of for loop,
.
.
.
(Hence, its continue till the condition not false).
We are run differ loop as our required pyramid program.
Just look out loop concept at:
http://cprogrammingcodes.blogspot.in/p/c-tutorial.html