Join 300,495 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,848 people online right now. Registration is fast and FREE... Join Now!
Hello! I am having problems making a program that demonstrates a loop that will print all the positive integers from 1 up to, but not including, the first number that is exactly divisible by both 30 and 42. End the "line" of output, and finish with a line of output saying: The smallest integer that is divisible by both 30 and 42 is ___. Here's what I have:
CODE
import java.util.Scanner;
public class Division { public static void main (String[ ] args) {
Scanner keyboard = new Scanner(System.in); int counter = 1; //this variable will hold successive positive integers... //...until one of them is divisible by both 30 and 42 while ((counter%30 != 0) || (counter%42 != 0)) { counter++; System.out.print("\t" + counter);//print the counter ;//add 1 to the counter } //end of while-loop //end the line of printing System.out.print("The smallest integer that is divisible by both 30 and 42 is " + counter);//print the final result
I am having problems with this program. I have to build a program that will just print a line that tells how many numbers between 1 and 1000 are exactly divisible by both 12 and 42. Don't actually print the numbers, just tell how many there are.will just print a line that tells how many numbers between 1 and 1000 are exactly divisible by both 12 and 42. Don't actually print the numbers, just tell how many there are. Here's what I have:
CODE
import java.util.Scanner;
public class DivisibleNumbers { public static void main (String[ ] args) {
Scanner keyboard = new Scanner(System.in); int counter; for (counter = 1; counter <=1000; counter++)//this variable will hold successive positive integers... //...until one of them is divisible by both 30 and 42 while ((counter%12 != 0) || (counter%42 != 0)) { System.out.print("\t" + counter);//print the counter ;//add 1 to the counter } //end of while-loop //end the line of printing System.out.print("The smallest integer that is divisible by both 30 and 42 is " + counter);//print the final result
int counter = 0; for(int i = 42; i <= 1000; i+=42) { if(i % 12 == 0 && i % 42 == 0) counter++; }
Another way to solve it is to find the LCM first. Then the answer is simply 1000 / lcm(12, 42)
Brilliant Dabtheman... but in that case, no need to check for i % 42
Both ways of doing it has greatly helped me and expanded my knowledge. If I wanted to have the program show also every single number divisible by both 30 and 42 before showing the smallest one, how would I do it? Here's what I have tried:
CODE
import java.util.Scanner;
public class DivisibleNumbers { public static void main (String[ ] args) {
Scanner keyboard = new Scanner(System.in);
int counter = 0;
for(int i = 1; i <= 1000; i++) { if(i % 30 == 0 && i % 42 == 0) System.out.print("\t" + counter); counter++; } System.out.print("\nThe smallest integer that is divisible by both 30 and 42 is " + counter);//print the final result
}//end main method }//end class
This post has been edited by villalandron: 1 Jul, 2009 - 07:54 PM
Once the program finds a number that is divisible by both 30 and 42, it stops. What I was asking was to have it show every single number that is divisible by both 30 and 42.
Once the program finds a number that is divisible by both 30 and 42, it stops. What I was asking was to have it show every single number that is divisible by both 30 and 42.
Just a thought - there are an infinite number of numbers divisible by 30 and 42. I would suggest implementing a way to make it stop at a specific number, or have the user do such?
If you add this after the for loop System.out.println("\nThe smallest integer that is divisible by both 30 and 42 is " + smallest);
That will print out all the numbers divisble by 30 and 42 up to 1000, and then print out the smallest number. No where in the little section of code I gave you does it stop after the first number...
If you add this after the for loop System.out.println("\nThe smallest integer that is divisible by both 30 and 42 is " + smallest);
That will print out all the numbers divisble by 30 and 42 up to 1000, and then print out the smallest number. No where in the little section of code I gave you does it stop after the first number...
What I mean is that it doesn't show all the numbers up to 1000 divisible by both 30 and 42. It shows 0,1,2, and 3. I know 6 is also divisible and so on.
Um.. you want numbers divisble by 30? or the common factors of 30 and 42?
Way you want is the common factors, which you would have to switch the loop around a bit. and counter is useless get rid of it.
CODE
int smallest = 0; int counter = 0;
for(int i = 1; i <= 42; i++) // changed to 42 because if over 42 there will always be a remainder { if(30% i == 0 && 42 % i == 0) // Switch the i and 30 and the i and 42 around { if(smallest == 0) smallest = i; System.out.print("\t" + i); } } System.out.println("The smallest is: " + smallest);
This post has been edited by Fuzzyness: 1 Jul, 2009 - 08:59 PM
Um.. you want numbers divisble by 30? or the common factors of 30 and 42?
Way you want is the common factors, which you would have to switch the loop around a bit. and counter is useless get rid of it.
CODE
int smallest = 0; int counter = 0;
for(int i = 1; i <= 42; i++) // changed to 42 because if over 42 there will always be a remainder { if(30% i == 0 && 42 % i == 0) // Switch the i and 30 and the i and 42 around { if(smallest == 0) smallest = i; System.out.print("\t" + i); } } System.out.println("The smallest is: " + smallest);
This is exactly what I was looking for, sorry for the lack of proper terminology. Probably could have found the answer 4 posts ago with proper terminology. Now, what would be the proper way to have another line show the count of how many common factors are there. For example: 30 and 42's common factors are 1 2 3 6. The total common factors is 4.
Um.. you want numbers divisble by 30? or the common factors of 30 and 42?
Way you want is the common factors, which you would have to switch the loop around a bit. and counter is useless get rid of it.
CODE
int smallest = 0; int counter = 0;
for(int i = 1; i <= 42; i++) // changed to 42 because if over 42 there will always be a remainder { if(30% i == 0 && 42 % i == 0) // Switch the i and 30 and the i and 42 around { if(smallest == 0) smallest = i; System.out.print("\t" + i); } } System.out.println("The smallest is: " + smallest);
This is exactly what I was looking for, sorry for the lack of proper terminology. Probably could have found the answer 4 posts ago with proper terminology. Now, what would be the proper way to have another line show the count of how many common factors are there. For example: 30 and 42's common factors are 1 2 3 6. The total common factors is 4.
Actually, nevermind on that, I was able to figure it out. The last program I need help with is a modification of the last one. I need to do print the sum of all the numbers between 1 and 1000 that are exactly divisible by both 12 and 42. Here's what I have so far:
CODE
import java.util.Scanner;
public class DivisibleNumbers { public static void main (String[ ] args) { Scanner keyboard = new Scanner(System.in);
int counter = 0;
for(int i = 1; i <= 42; i++) { if(30 % i == 0 && 42 % i == 0) { counter++; } } System.out.print("\nThe total amount of numbers divisible by 30 and 42 are: " + counter);//print the final result
We have done the major portion of your homework. The least thing you can do is finish it up yourself.
What is the point of taking a class if you learn absolutely nothing and ask others do your work? Because I can assure you, that's not what you should be doing.
We have done the major portion of your homework. The least thing you can do is finish it up yourself.
What is the point of taking a class if you learn absolutely nothing and ask others do your work? Because I can assure you, that's not what you should be doing.
On the contrary, I have learned so much on these exchanges of posts that I could easily perform these exercises again with no problems.
Well take what you have learned from these posts and evolve it into what you need. Seeing a couple different methods of doing things from these last few posts im sure you will be able to get it. If you have any Specific questions, post the code you have come up with and the question and you shall recieve advice on how to fix it. Dan is right as to us giving you code. I shall try to explain in words now and not as much in code.