Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

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!




Integers divisible by 12 and 42

 

Integers divisible by 12 and 42

villalandron

1 Jul, 2009 - 05:59 PM
Post #1

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

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
        
   }//end main method
}//end class



Thank you for your help!

User is offlineProfile CardPM
+Quote Post


villalandron

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 06:05 PM
Post #2

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

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
        
   }//end main method
}//end class



Thank you for your help!
User is offlineProfile CardPM
+Quote Post

pbl

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 06:12 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
Ouf... not that complicated
CODE

int counter = 0;
for(int i = 1; i <= 1000; i++) {
    if(i % 12 == 0 && i % 42 == 0)
       counter++;
}


Duplicated topics mad.gif merged
User is offlineProfile CardPM
+Quote Post

Dantheman

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 06:12 PM
Post #4

D.I.C Regular
***

Joined: 27 May, 2009
Posts: 445



Thanked: 25 times
My Contributions
I'd rather increment the counter by 42.

CODE

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)

This post has been edited by Dantheman: 1 Jul, 2009 - 06:16 PM
User is offlineProfile CardPM
+Quote Post

pbl

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 06:18 PM
Post #5

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
QUOTE(Dantheman @ 1 Jul, 2009 - 06:12 PM) *

I'd rather increment the counter by 42.

CODE

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 icon_up.gif tongue.gif
User is offlineProfile CardPM
+Quote Post

villalandron

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 07:53 PM
Post #6

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

QUOTE(pbl @ 1 Jul, 2009 - 06:18 PM) *

QUOTE(Dantheman @ 1 Jul, 2009 - 06:12 PM) *

I'd rather increment the counter by 42.

CODE

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 icon_up.gif tongue.gif


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
User is offlineProfile CardPM
+Quote Post

Dantheman

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 07:58 PM
Post #7

D.I.C Regular
***

Joined: 27 May, 2009
Posts: 445



Thanked: 25 times
My Contributions
Hint: The time that counter is getting incremented is the time your program has the knowledge of occurrence of such divisible number.
User is offlineProfile CardPM
+Quote Post

Fuzzyness

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 08:00 PM
Post #8

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,149



Thanked: 173 times
Dream Kudos: 150
My Contributions
That will show you every number. to get the smallest you will have to have a variable assigned to smallest one.
CODE

int smallest = 0;
int counter = 0;
        
        for(int i = 1; i <= 1000; i++)
        {
           if(i % 30 == 0 && i % 42 == 0)
           { if(smallest == 0) smallest = counter;
           System.out.print("\t" + counter);    
           counter++;
           }
         }

User is offlineProfile CardPM
+Quote Post

villalandron

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 08:18 PM
Post #9

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

QUOTE(Fuzzyness @ 1 Jul, 2009 - 08:00 PM) *

That will show you every number. to get the smallest you will have to have a variable assigned to smallest one.
CODE

int smallest = 0;
int counter = 0;
        
        for(int i = 1; i <= 1000; i++)
        {
           if(i % 30 == 0 && i % 42 == 0)
           { if(smallest == 0) smallest = counter;
           System.out.print("\t" + counter);    
           counter++;
           }
         }




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.
User is offlineProfile CardPM
+Quote Post

wolfman29

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 08:25 PM
Post #10

New D.I.C Head
*

Joined: 24 Jun, 2009
Posts: 46



Thanked: 1 times
My Contributions
QUOTE(villalandron @ 1 Jul, 2009 - 08:18 PM) *

QUOTE(Fuzzyness @ 1 Jul, 2009 - 08:00 PM) *

That will show you every number. to get the smallest you will have to have a variable assigned to smallest one.
CODE

int smallest = 0;
int counter = 0;
        
        for(int i = 1; i <= 1000; i++)
        {
           if(i % 30 == 0 && i % 42 == 0)
           { if(smallest == 0) smallest = counter;
           System.out.print("\t" + counter);    
           counter++;
           }
         }




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?

User is offlineProfile CardPM
+Quote Post

Fuzzyness

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 08:28 PM
Post #11

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,149



Thanked: 173 times
Dream Kudos: 150
My Contributions
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...
User is offlineProfile CardPM
+Quote Post

villalandron

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 08:51 PM
Post #12

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

QUOTE(Fuzzyness @ 1 Jul, 2009 - 08:28 PM) *

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.
User is offlineProfile CardPM
+Quote Post

Fuzzyness

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 08:57 PM
Post #13

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,149



Thanked: 173 times
Dream Kudos: 150
My Contributions
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
User is offlineProfile CardPM
+Quote Post

villalandron

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 09:09 PM
Post #14

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

QUOTE(Fuzzyness @ 1 Jul, 2009 - 08:57 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.
User is offlineProfile CardPM
+Quote Post

villalandron

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 09:23 PM
Post #15

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

QUOTE(villalandron @ 1 Jul, 2009 - 09:09 PM) *

QUOTE(Fuzzyness @ 1 Jul, 2009 - 08:57 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.


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



User is offlineProfile CardPM
+Quote Post

Dantheman

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 09:28 PM
Post #16

D.I.C Regular
***

Joined: 27 May, 2009
Posts: 445



Thanked: 25 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

villalandron

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 09:31 PM
Post #17

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

QUOTE(Dantheman @ 1 Jul, 2009 - 09:28 PM) *

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.
User is offlineProfile CardPM
+Quote Post

Fuzzyness

RE: Integers Divisible By 12 And 42

1 Jul, 2009 - 09:53 PM
Post #18

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,149



Thanked: 173 times
Dream Kudos: 150
My Contributions
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.

Thanks Dan, forgot there for a minute smile.gif

Goodluck.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:52AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month