Monday, 30 January 2012

Making triangle of stars using loops

*
**
***
****
*****
  1. Start first loop with a variable name lets say "a" from 1 to5  and increment it by 1 and write starting  curly braces of it.
  2. Start second loop with a variable name lets say "b" from 1 to less then a and increment it by 1 and this loop should be the nested of first loop.
  3. Write starting curly braces of second loop  and then write output statement which prints * like ....................print("*"); ( note: don't write println here) and then write ending curly braces of second loop.
  4. Write statement for line break outside the body of second loop and inside the body of first loop like ...................println(); and then write ending curly braces of the first loop.
*****
****
***
**
*

  1. Start first loop with a variable name lets say "a" from 1 to 5 and increment it by 1 and write starting  curly braces of it.
  2. Start second loop with a variable name lets say "b" from 5 to greater then a and decrement it by 1 and this loop should be the nested of first loop.
  3. Write starting curly braces of second loop  and then write output statement which prints * like ....................print("*"); ( note: don't write println here) and then write ending curly braces of second loop.
  4. Write statement for line break outside the body of second loop and inside the body of first loop like ...................println(); and then write ending curly braces of the first loop.
        *
      **
    ***
  ****
*****

  1. Start first loop with a variable name lets say "a" from 1 to 5 and increment it by 1 and write starting curly braces of it.
  2. Start second loop with a variable name lets say "b" from 5 to greater then a and decrement it by 1 and this loop should be the nested of first loop.
  3. Write starting curly braces of second loop and then write output statement which prints one space like ....................print(" "); ( note: don't write println here) and then write ending curly braces of second loop.
  4. Start third loop with a variable name lets say "c" from 1 to less then a and increment it by 1 and this loop should also be the nested of first loop but not of second loop.
  5. Write starting curly braces of third loop and then write output statement which prints * like ....................print("*"); ( note: don't write println here) and then write ending curly braces of third loop.
  6. Write statement for line break outside the body of second loop and third loop but inside the body of first loop like ...................println(); and then write ending curly braces of the first loop.
*****
  ****
    ***
      **
        *

  1. Start first loop with a variable name lets say "a" from 1 to 5 and increment it by 1 and write starting curly braces of it.
  2. Start second loop with a variable name lets say "b" from 1 to less then a and increment it by 1 and this loop should be the nested of first loop.
  3. Write starting curly braces of second loop and then write output statement which prints one space like ....................print(" "); ( note: don't write println here) and then write ending curly braces of second loop.
  4. Start third loop with a variable name lets say "c" from 5 to greater then a and decrement it by 1 and this loop should also be the nested of first loop but not of second loop.
  5. Write starting curly braces of third loop and then write output statement which prints * like ....................print("*"); ( note: don't write println here) and then write ending curly braces of third loop.
  6. Write statement for line break outside the body of second loop and third loop but inside the body of first loop like ...................println(); and then write ending curly braces of the first loop.

No comments:

Post a Comment