//program to display following numeric/star pattern.
// 1 1 1 1 1
// 2 2 2 2 2
// 3 3 3 3 3
// 4 4 4 4 4
#include <stdio.h>
#include<conio.h>
void main() { int i,j; for(i=1;i<=4;i++) { for(j=1;j<=5;j++) { printf(" %d ",i); } printf("\n"); } getch(); }
-----------------------------
1)we have to display 1 1 1 1 1,then 2 2 2 2 2 2 ,and so on. we use loop for this.
// 1 1 1 1 1
// 2 2 2 2 2
// 3 3 3 3 3
// 4 4 4 4 4
#include <stdio.h>
#include<conio.h>
void main() { int i,j; for(i=1;i<=4;i++) { for(j=1;j<=5;j++) { printf(" %d ",i); } printf("\n"); } getch(); }
-----------------------------
Logic in mind:
----------------------
----------------------
1)we have to display 1 1 1 1 1,then 2 2 2 2 2 2 ,and so on. we use loop for this.
We have to know one thing here that, there are four rows and five columns. so for this, we use nested loop. The outer loop is for rows and inner loop is for columns.
2)accordingly we set up the loops.since the displays are in repetitive form ,,we set up the loop in nested form such that
internal loop executes first time with display of numbers(above) taken from outer loop and that is '1' first and '2' ...,
five times
.3)First loop(outer) takes value 1 and assigns to display unit(printf).The inner executes five times with display taken from outer loop.
4)next time, the outer loop takes the value 2 and transfers to display unit. the inner loop displays the number again and again(five times)and it goes on.
............................................................................................
if you want to display "*" then use "*" in printf function.
2)accordingly we set up the loops.since the displays are in repetitive form ,,we set up the loop in nested form such that
internal loop executes first time with display of numbers(above) taken from outer loop and that is '1' first and '2' ...,
five times
.3)First loop(outer) takes value 1 and assigns to display unit(printf).The inner executes five times with display taken from outer loop.
4)next time, the outer loop takes the value 2 and transfers to display unit. the inner loop displays the number again and again(five times)and it goes on.
............................................................................................
if you want to display "*" then use "*" in printf function.