0

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+ " ");


      }
    }
  }
}

Edited by mike_2000_17: Fixed formatting

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Eric Cute
1

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 + " ");
		}
0

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 + " ");
}
}
}

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.