ok i just started loops in my java class and im stuck on getting this loop working i dunno what all math to do to get these numbers for the output
97, 84, 72, 61, 51, ... 1

Recommended Answers

All 6 Replies

try this...

int diff=14
for(int lp=97;lp>0;lp-=diff)
{
System.out.println(lp);
diff--;
}

try that, think it should work

The solution that you were given does not seem to work if I use it, so I decided to use another method using a while loop and the instructions are as follows

int i = 97;
int j = 14;

while(j > 0)
{
System.out.println(+ i);
j--;
i = i - j;
}
This solution will definately work, try it and you will see.

The first implementation doesnt work because diff ends up becoming negative before the value reaches 0. Should be an infinite loop if my quick glance is correct.

Well I just took a look at the program...
The numbers 97, 84, 72, 61, 51, ... 1 are wrong.
It doesn't end at 1, and it doesnt actually go negative, which is why my loop is infinite.
So depends where you want the loop to end, the smallest it gets to is 6. So you could end it at 6.

ok i just started loops in my java class and im stuck on getting this loop working i dunno what all math to do to get these numbers for the output
97, 84, 72, 61, 51, ... 1

class Loops
{
public static void main(String[] args)
{
for(int i=97;i>0;i--)
{
for(int j=13;j>0;j--)
{
System.out.print(i+" ");
i=i-j;
//System.out.println(j);
}
break;
}
}
}

this is my ID: bommavj@gmail.com

my name is vijay if u wanna any help send me queries...


yours vijay....

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.