2

I'm working on some homework for my Intro to Programming class and one of the questions is

Write a program that displays all the integers from 1 to 1000 that are not evenly divisible by 13. (Hint: x is not evenly divisible by 13 if the expression x % 13 ! = 0 is true. Recall that % is the remainder operator.) My train of thought was that what I want the program to do is to take x, whatever it may be, if it's less than 1000, then divide it by 13 and if the remainder is not 0 then display the number. If the remainder is 0, don't display the number.

My first attempt was the following,

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

    while (x < 1000)
        {
            System.out.println(x);  
            x++;
        }
    }
}

but when it runs it simply prints the numbers 1 through 999. I'm thinking maybe this needs to be an if/else statement but I'm unsure as to what the "else" parameters would be.

My book gives this as an example:

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

        while (x*x < 5000)
            {
            System.out.println(x + " squared = " + x*x);
            x++;
            }
     }
}

And of course it worked perfectly.

The program I'm supposed to be writing isn't quite the same as the example given in the book but it's the same principle, and I've been playing around with this for a while and can't seem to identify the issue.

I'm sure this is a really simple question with an even simpler answer but I'm brand-spanking new to programming and I'm a bit lost.

Any ideas?

Dariani Disani
  • 79
  • 2
  • 3
  • 9
  • 6
    Neither of your programs mentions the number 13...? – Louis Wasserman Sep 19 '13 at 22:04
  • 1
    Where is your division by 13 ? Or your modulo 13 ? – Erwin Smout Sep 19 '13 at 22:05
  • 2
    Did you read the hint? It's quite important. You need an `if` statement with that test in there before outputting. – paddy Sep 19 '13 at 22:05
  • `while (..) { if (!divisbleBy13(x)) { .. } }`. Now just to figure out `!divisibleBy13(x)` .. which is given in the hints. – user2246674 Sep 19 '13 at 22:06
  • Seriously, why do questions like this get up-votes? :> – user2246674 Sep 19 '13 at 22:08
  • 2
    @user2246674 what is the problem with this question? Did you have a JCP certification when you came out of your mother's womb? No one is born a programmer. Also this question meets all the standards of Stack Overflow. – Geeky Guy Sep 19 '13 at 22:12
  • Thanks to everyone who down-voted my answer. Maybe we should just give everyone hints to their answer. I don't remember it saying anywhere that "answers to homework questions should not give the answer, it should only give direction and hints". Honestly, this is a double standard. If the OP asked for direction, that's one thing, but it didn't... – BlackHatSamurai Sep 19 '13 at 22:14
  • @Renan I never said it was an awful question nor did I imply that down-votes were applicable. I merely remarked that I find that the up-votes given are not warranted. Leaving it at 0 (and providing answers) is a perfectly acceptable way to handle such cases. – user2246674 Sep 19 '13 at 22:55

2 Answers2

3

What your program lacks is the "is the number not divisible by 13" check.

Remember, when you write an if statement, whatever is in its corresponding block will only be executed if the expression between the parenthesis is true. So inside your while loop, write an if statement, and only print the number if it is not divisible by 13. Like this:

while (x <= 1000) // notice the <= operator
{
    if (/*change this multiline comment for an expression
        that checks wheter x is not divisible by 13*/) {
        System.out.println(x);
    }
    x++; // Increment x whether it is divisible or not.
}

I would write the actual expression there, but then I'd be answering your homework for you. I just wish you get the logic for this, so you can then answer your homework by yourself - and internalize the knowledge. May it be that this is useful for you.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
-2

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

while (x < 1000)
    {
        System.out.println(x);  
        System.out.println(x+1);  
        System.out.println(x+2);  
        System.out.println(x+3);  
        System.out.println(x+4);  
        System.out.println(x+5);  
        System.out.println(x+6);  
        System.out.println(x+7);  
        System.out.println(x+8);  
        System.out.println(x+9);  
        System.out.println(x+10);  
        System.out.println(x+11);  
        x+=13;
    }
}

}

But there's still some tweaking to be done to make this halt at the number 1000.

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52