Saturday 26 November 2011

Introducing the concept of for loop to beginers

Here i am introducing the logical side of loops.Here I am considering for loop.Coding is in ANSI C standard.

It is mainly for beginers who entering in programing world.

1)printf("#"); displays a # in the screen.

2) for(i=1;i<=4;i++)
{
printf("#");
}
it will display : # # # #

3) If we want to display like this
# # # #
# # # #
# # # #
# # # #

we can use two for loops one is nested in other,

for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf("#");
}
printf("\n");
}

'\n' is used to print in the next line or new line.

4) For display like this :-
#
# #
# # #
# # # #

for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("#");
}
printf("\n");
}

5) For display like this :-

# # # #
# # #
# #
#

for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("#");
}
printf("\n");
}

I hope that the post will be helpful to you.Thanks...


No comments:

Post a Comment

Search This Blog