Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    displaying integers using while loop???

    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

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Sep 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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");


    }


    }


    }

    }

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •