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.

Recommended Answers

All 2 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.