Saturday, November 14, 2009

For Loop Triangle Program

#include
#include
main()
{
int row,col;
int n = 0;
printf ("Please enter the number: ");
scanf ("%d",&n);
for (col = 1; col >= n; ++col) /*Loop For column */
{
for (row = 1 ; row >= col ; ++row) /*Loop For row */
{
printf ("*");
}
printf ("\n");
}
getch();
}

OUTPUT:
Please enter the number:4

*
**
***
****

Explanation:

1) In the above program we have taken two variables row and col to display row and column. Geometrically tringle is two dimensional figure so we have to take two diffrent varibles to deal with row and column.

2) Now, we are asking user to enter the number.

3) First for loop, i.e. outer loop is to deal with the column.
for (col = 1; col <= n; ++col) here it is initialized with 1 and compared with the value enterd by user so that it should not exceed the entered value and incrimented to jump to next column as soon as the first row get printed. 4) The second or inner for loop is dealing with the row and hence it is the loop which in fact prints the starts '*'. Ultimately rows on multiple lines causes the columns. 5) At first soon you enter the value for n, say 4. The outer for loop will check the condition. for (col = 1; 1 <= 4; ++col) Found true and hence execution pointer will move to inner for loop and check the condition for (row = 1 ; 1 <= 1; ++row) Found true, so it will execute print statement, now as incrimentation is there the value of row is two and hence condition false. it will again go to outer for loop as it is not over. The value of col is now 2. for (col = 1; 2 <= 4; ++col) Found true and hence execution pointer will move to inner for loop and check the condition for (row = 1 ; 1 <= 2; ++row) Found true, so it will execute print statement.Now as incrimentation is there the value of row is two for (row = 1 ; 2 <= 2; ++row) again condition is true so print statement will get executed again and result in two '*' on same row. This way it will continue till outer for loop gives false result for condition. Let us reverse the triangle, #include
#include

main()
{
int row,col;
int n = 0;
printf ("Please enter the number: ");
scanf ("%d",&n);
for (col = n; col >= 1; --col) /*Loop For column */
{
for (row = col ; row >= 1 ; --row) /*Loop For row */
{
printf ("*");
}
printf ("\n");
}
getch();
}

OUTPUT:
Please enter the number:4

****
***
**
*

// a program to show the nested for loops

#include



int main()

{

// variables for counter…

int i, j;

// outer loop, execute this first...

// for every i iteration, execute the inner loop

for(i=1; i<10;)

{

// display i

printf("%d", i);

// then, execute inner loop with loop index j the initial value of j is i + 1

for(j=i+1; j<10; )

{

// display result of j iteration

printf("%d", j);

// increment j by 1 until j<10

j = j + 1;

}

// go to new line

printf("\n");

// increment i by 1, repeat until i<10

i = i + 1;

}

return 0;

}



The program has two for loops. The loop index i for the outer (first) loop runs from 1 to 9 and for each value of i, the loop index j for the inner loop runs from i + 1 to 9. Note that for the last value of i (i.e. 9), the inner loop is not executed at all because the starting value of j is 10 and the expression j < 10 yields the value false because j = 10.

The following is the flowchart for the above program.


Followers