0
class Stars

{
public static void main(String args[])
{
    int c=1;

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

{
    for(int k=1;k<=c;k++)
{
    if(k%2==0)

    System.out.print("*");

   // else

   // System.out.print("*");
}
    System.out.println();
    c+=2;
}

output:

*
**
***
****
*****
******
*******
********
*********

i cannot understand that what the function of variable "c" and how it print star by increment.can someone explain it to me.

Edited by mike_2000_17: Fixed formatting

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by NormR1
0

The code you show does not produce the output you show. Show us matching code and output. Post code, properly indented, in code tags.

0

One way to understand a simple loop like you show is to play computer with it. Manually step thru the code and compute variables as they change and write down output as it is printed.
Another way is to add some debugging print statements to show how the values change and how the code flow goes to show you what the code is doing.

Of course another way yet is let someone explain it to you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.