i need to get the output of

1 2  4  7  11  16  22

i know im on the right track however i cant figure out a way to get the outputs in the right form like 1+2+3 etc like its doing in the output i should get.

i know im a step or two off probably but any advice would be nice = )

my code reads:

public class assign6_4
{
  public static void main (String [] args)
  {
    for(int x=0; x<1; x++)
    {
      for(int j= 1; j<=22; j=j+2)
      {
        System.out.print(j+ " ");


      }
    }
  }
}

The difference between two adjacent numbers in the series of numbers increases one each time. Therefore, one has to update the difference by one increment each time:

difference++

Hence one may calculate the next number by the following code:

num +=difference++;

Therefore the for each loop could be written as follows:

int difference=0, num=1;
   for(int j= 0; j<22; j++){
   num +=difference++; 
   System.out.print(num + " ");
		}

thanks for much for that. was able to figure it out from that. final code ended up as.

public class assign6_4
{
public static void main (String [] args)
{
int i=0, x=1;
for(int j= 0; j<7; j++)
{
x +=i++;
System.out.print(x + " ");
}
}
}

We're glad you figured it out. Now please mark this thread as SOLVED.

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.