2

I am a student trying to learn programming on my own, getting help from online sources and people like you. I found an exercise online to create a small program that will do this:

Write a program that will read the numbers a and b (type long) and list how many numbers between a and b are divisible by either 2, 3 or 5.

For example:

a=11 b=30 The counter would be 14, since there are 14 numbers divisible by 2,3 or 5 in between: 12, 14, 15, 16, 18, 20, 21, 22, 24,25, 26, 27, 28, 30

This is what I have already tried, but it doesn't seem to work. I would need your guidance and help to finish this. Thank you for your time and your hard work in advance.

import java.util.Scanner;

public class V {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long a = sc.nextLong();
        long b = sc.nextLong();

        for (long c = a; c <= b; c++) {
            if (c%2 || c%3 || c%5) {
                System.out.println(c);
            }
        }
    }
}

CURRENT STAGE OF THE PROGRAM:

import java.util.Scanner;

public class Test2 {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    long a = sc.nextLong();
    long b = sc.nextLong();

    long count = 0; // counter

    for (long c = a; c <= b; c++) {
        if (c % 2 == 0 || c % 3 == 0 || c % 5 == 0) {
            count++;
            System.out.println(c);
            }

        }
    }

}

There is still a thing to do:

Now it lists me the numbers which are divisible by 2,3 or 5. But all I need is one single number that will count how many numbers there is.

Killian19
  • 23
  • 6
  • "it doesn't seem to work", can you be a bit more specific ? what doesn't work? and do you get an error message ? – Stultuske Oct 31 '14 at 13:24
  • yes "c" is 14 and it holds the number that we want in the end. It is the counter.. "how many numbers are divisible by the given three numbers" – Killian19 Oct 31 '14 at 13:26
  • @Stultuske: it gives me the error of bad operand types if I use || , but I don't know any other way of using this. – Killian19 Oct 31 '14 at 13:27
  • if (c%2 || c%3 || c%5) c%2 is not an expression that leads to true or false, it's not a boolean, it returns an int. so, what you wrote there, is something similar as: if ( 2 OR 1 OR 0 ) in the other snippet you have: if ( c%2==0 ) this does return either true or false, so there you are able to use the OR operand – Stultuske Oct 31 '14 at 13:59
  • If it's asking you to list the amount that is divisible by 2, the amount divisible by 3, and the amount divisible my 5, you need three separate variables and 3 separate if statements. If it's asking you to keep track of those numbers use an ArrayList to store them. – Luminous Oct 31 '14 at 13:59
  • It only asks me to lists the amount of numbers that are divisible by either 2,3 or 5. For example: if a= 5 and b=8 it would return the number 3....why? because 5,6 and 8 are divisible by either 2,3 or 5. – Killian19 Oct 31 '14 at 14:03

6 Answers6

5

You're on the right way - you just need to turn your modulo expression into condition - i.e., check that the remainder is actually 0. Also, you should separate between the loop variable and the counter of the results:

long c = 0; // counter
for(long l = a; l <= b; l++) {
    if (l % 2 == 0|| l % 3 == 0 || l % 5 == 0) {
        ++c;
    }
}
System.out.println(c);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
3

c%2 returns a integer, not a boolean.

You have to check if (c%2==0 || ...)

Terry Storm
  • 497
  • 3
  • 14
  • Not only this is the issue OP needs count. – SMA Oct 31 '14 at 13:31
  • @almas shaikh you are correct, however the most crucial part is within the if-part. It is a "helping" forum, not "solving every little detail" forum. from here on out i m certain he can work out the rest himsself – Terry Storm Oct 31 '14 at 13:36
3

I would say it does not even compile:

if(c%2 || c%3 || c%5)

c%2 deos not evaluate to a boolean in java, unlike in C or C++;

You have to use the equal operator ==

if(c%2 == 0 || c%3 == 0 || c%5 == 0)
A4L
  • 17,353
  • 6
  • 49
  • 70
  • Not only this is the issue OP needs count. – SMA Oct 31 '14 at 13:31
  • @almasshaikh OP didnt even get so far to have a wrong count, he probably wanted to count the lines printed, who knows ;-) – A4L Oct 31 '14 at 13:34
0

Try this:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    long a = sc.nextLong();
    long b = sc.nextLong();

    int count = 0; // counter

    for (long c = a; c <= b; c++) {
        if (c % 2 == 0 || c % 3 == 0 || c % 5 == 0) {//this where the error is in your program
            count++;
            System.out.println(c);//you are printing c and you are not maintaining the counter
        }
    }
    System.out.println("Counter is " + count);
}
SMA
  • 36,381
  • 8
  • 49
  • 73
  • Hey I tried cour concept and it works as it should but there is one problem... It gives me the list of those numbers divisible by 2,3 or 5... I just want the number that will count all those numbers.. FOR EXAMPLE: it gives me 14 numbers, but I just want the number 14 listed. – Killian19 Oct 31 '14 at 13:34
  • @Killian19 Remove the line `System.out.println(c)` – A4L Oct 31 '14 at 13:36
  • I have done that and it now gives me :counter is 1, counter is 2, counter is 3.......counter is 14 – Killian19 Oct 31 '14 at 13:42
0

Try this code:

 for(int i=a;i<=b;i++){
  if(i%2 == 0 || i%3 == 0 || i%5 == 0){
    c++;
  }
 }
AsSiDe
  • 1,826
  • 2
  • 15
  • 24
0

I would go back and learn about modular arithmetic. Assuming c = 9, c%5 = 4. How is that suppose to be interpreted as a boolean?

In your code you could have an expression like "if(c%2 || c%3 || c%5)" which, assuming c=9, translates to if(1 || 0 || 4). In Java, conditionals need to be written as boolean expressions. You need to write them so your answers can explicitly be true or false.

The right way would be:

if((c % 2 == 0) || (c % 3 == 0) || (c % 5 == 0))

As for counting the number of numbers, use a separate variable and increment if the conditional is true.

Luminous
  • 1,771
  • 2
  • 24
  • 43
  • Hey thank you! I will read about the modular arithmetics. Thanks !!! :) ... I have done the if conditionals now, but I am still struggeling for counting the number of numbers. I have used a separate variable but it only gives me the list of those numbers. I have posted the updated code on my question post if you could please take a look. – Killian19 Oct 31 '14 at 13:52
  • @Killian19 Read the last line in my answer. – Luminous Oct 31 '14 at 14:01
  • I have done that, but now it gives me all the numbers not just the final one. IT prints me 1,2,3,4,...,14 What am I doing wrong? – Killian19 Oct 31 '14 at 14:07
  • @Killian19 Do you know what `System.out.println(c);` is doing? Figure that out. – Luminous Oct 31 '14 at 14:09
  • Oh I have figureed it out. I had put the System.out.println in the FOR loop as opposed to outside the loop. I have fixed the problem now. Thank you! – Killian19 Oct 31 '14 at 14:09
  • @Killian19 Your welcome! =) don't forget to mark the question that helped you the most as your answer – Luminous Oct 31 '14 at 14:10
  • and I removed the System.out.println(c), which obviously printed the numbers divisible by 2,3,5 – Killian19 Oct 31 '14 at 14:10
  • but why doesnt it work if I put this as the a and b. a=123456789012345678 ...b=876543210987654321 ... it doesnt even give me an answer – Killian19 Oct 31 '14 at 14:16
  • @Killian19 Go read about the differences between [int, long, double, float, byte, short](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html), and BigInteger. Saying it doesn't work tells me nothing about what's going on. Using StackOverflow(SO) is great, but go do your own research before you come here. This is not the first place you come to when you have a question. It is the last place. Seriously. You'll be better off that way. – Luminous Oct 31 '14 at 14:25
  • I mean it doesnt run at all. It gives me no output . Yes I know thr diffrence but '' long'' should do it.. Shouldnt it? – Killian19 Oct 31 '14 at 16:00
  • Debug and step through the code. Watch what happens. – Luminous Oct 31 '14 at 16:44
  • I had done that... I tried changing the types and all the results are the same. It just isn't doing anything. From what I understand is that it would need to take a long time to check it...but I don't know how to fix it. I haven't dealt with any problem similar to this. – Killian19 Oct 31 '14 at 16:49
  • When i debug it it gives me the error : Main.java:3: error: class Test is public, should be declared in a file named Test.java public class Test { http://ideone.com/6wEVj1 – Killian19 Oct 31 '14 at 17:01
  • @Killian19 You can't fix how long it takes for your code to run. Your two numbers are checking 753,086,421,975,308,643 numbers. That's 753 quadrillion numbers. Did you honestly expect for it to run in a couple of seconds? It's not doing anything because it's still running. – Luminous Oct 31 '14 at 17:29
  • There must be a quicker way then, because there is a solution to that exercise. It's advanced yes...but there is a way where the program will check it faster....is there anyway to modify the code so it doesn't run through that many numbers so the code finishes quicker? – Killian19 Oct 31 '14 at 17:33
  • @Killian19 What's the solution in the exercise? The exact number I mean. – Luminous Oct 31 '14 at 18:22
  • 552263376115226339 is the solution for the a=123456789012345678 b=876543210987654321 – Killian19 Oct 31 '14 at 18:32
  • @Killian19 I'll give a hint then I'm done helping. Take the difference of A and B , divide by 2. Take the difference of A and B, divide by 3. Take the difference of A and b and divide by 5. Round these numbers down. Now you have the amount of numbers divisible by the sets {2},{3},{5},{2,3},{2,5},{3,5},and {2,3,5}. Your job now is to figure out how to **remove** the amount of numbers that are divisible by the sets {2,3},{2,5},{3,5},and {2,3,5}. This will leave you with the amount of #s divisible by the sets {2}, {3}, and {5}. Good luck! If you want help on this go to the math stack exchange! – Luminous Oct 31 '14 at 19:39
  • Thank you for your help really! You are a savor!... I will try to figure it out on my own, but with my luck the last hour... I will try to use the math exchange as well! thank you Luminous – Killian19 Oct 31 '14 at 21:46