//program to display given star/number pattern
// 1 1 1 1 1
// 2 2 2
// 3
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,sp=5,m=1;
for(i=5;i>=1;i=i-2)
{
for(k=sp;k>=i;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf(" %d ",m);
}
printf("\n");
m++;
}
getch();
}
---------------------------------
logics in mind
-------------------------------
1)we have to display 1 1 1 1 1 so we use loop.
2)since the displays are in repetitive form ,,we set up the loop in nested form such that
internal loop executes 5 times with display of numbers(above),then 3 times and then 1 time.
3)if we look carefully to given pattern, we can see there space increased after each display. for this, we have used loop. in each display, it increases space and then it goes for next display.
3)First loop(outer) takes value 5 and assigns to inner.The inner executes 5 times with display.
4)next time, the outer loop takes the value 3 and transfers to inner loop, it displays again and again.
and it goes on
-----------
note:
--------------
if you want to display "*" (star) then use "*" in printf function.
// 1 1 1 1 1
// 2 2 2
// 3
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,sp=5,m=1;
for(i=5;i>=1;i=i-2)
{
for(k=sp;k>=i;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf(" %d ",m);
}
printf("\n");
m++;
}
getch();
}
---------------------------------
logics in mind
-------------------------------
1)we have to display 1 1 1 1 1 so we use loop.
2)since the displays are in repetitive form ,,we set up the loop in nested form such that
internal loop executes 5 times with display of numbers(above),then 3 times and then 1 time.
3)if we look carefully to given pattern, we can see there space increased after each display. for this, we have used loop. in each display, it increases space and then it goes for next display.
3)First loop(outer) takes value 5 and assigns to inner.The inner executes 5 times with display.
4)next time, the outer loop takes the value 3 and transfers to inner loop, it displays again and again.
and it goes on
-----------
note:
--------------
if you want to display "*" (star) then use "*" in printf function.
No comments:
Post a Comment