Reputation: 23
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.
Upvotes: 2
Views: 664
Reputation: 1909
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.
Upvotes: 0
Reputation: 311163
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);
Upvotes: 5
Reputation: 1846
Try this code:
for(int i=a;i<=b;i++){
if(i%2 == 0 || i%3 == 0 || i%5 == 0){
c++;
}
}
Upvotes: 0
Reputation: 37023
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);
}
Upvotes: 0
Reputation: 17595
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)
Upvotes: 3
Reputation: 497
c%2
returns a integer, not a boolean.
You have to check if (c%2==0 || ...)
Upvotes: 3