...

displaying integers using while loop???

wanye
01-21-2003, 02:39 PM
13. Write a java program that can display the following integers from 1 to 30 using a while loop and the counter variable x. Assume that the variable x has been declared but not initialized. Print only five integers per line. [Hint: Use the calculation x % 5. When the value of this is 0, print a newline character; otherwise, print a tab character. Assume this is an application–use the System.out.println() method to output the newline character and use the System.out.print( "\t" ) method to output the tab character.]
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30

Spookster
01-21-2003, 03:51 PM
We are not going to do your homework for you. Do it yourself and then if you have specific questions about java syntax we will answer those questions.

wanye
01-21-2003, 04:35 PM
is e code something like this?I compile it and run but i juz dunno where went wrong... can anyone help me wif it?



public class Tut2
{
public static void main(String[] args)
{
int x=1;


while(x<=30)
{


System.out.print(x);

System.out.print("\t");

x=x%5;
x++;
if(x==0)
{

System.out.println("\n");


}


}


}

}

Spookster
01-21-2003, 04:50 PM
Now in your code to print the newline character you are checking x to see if it is 0 (after moduloing it by 5) which it should be after every 5th integer. Look at your code here:

x=x%5;
x++;
if(x==0)

The problem is pretty obvious assuming you know what x++ does.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum