Input an integer number and display its largest factor

Reply

Join Date: Oct 2009
Posts: 15
Reputation: kulrik is an unknown quantity at this point 
Solved Threads: 0
kulrik kulrik is offline Offline
Newbie Poster

Input an integer number and display its largest factor

 
-1
  #1
26 Days Ago
Alright guys i got another one:

In the class Mymath, create a public method called calculateFactor. The method has two two arguments of type integer and checks whether the smaller of the two integers is a factor of the larger of the two integers. The method will return a factor or a zero.

Write a main method which reads in a positive integer n and displays out the largest proper divisor of n (that is, the largest factor of n that is less than n itself).

Sample output:

Input an integer number:
124
Largest divisor: 62
Another input (y/n)?
y
Input an integer number:
18
Largest divisor: 9

Sample Run:

6
28
496

Im having alot of trouble with this one. Please Help.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 47
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
-1
  #2
25 Days Ago
hi
for the first problem you could do something like this:

  1. public class Mymath {
  2.  
  3. public calculateFactor( int a, int b ) {
  4. if ( a < b && b % a == 0 )
  5. return true;
  6. elif ( a > b && a % b == 0 )
  7. return true;
  8. else
  9. return false;
  10. }
  11. }

and for the second part see if this is any helpful :

  1. import java.util.Scanner;
  2.  
  3. public class Test {
  4.  
  5. public static void main( String[] args ) {
  6. Scanner in = new Scanner( System.in );
  7. String goOn = "y";
  8. int n, largest = 0;
  9. do {
  10. System.out.println( "Input an integer number:" );
  11. n = in.nextInt();
  12.  
  13. for ( int i = 1; i < n; i++ )
  14. if ( n % i == 0 )
  15. if ( i > largest )
  16. largest = i;
  17. System.out.println( "Largest divisor: " + largest );
  18. System.out.println( "Another input (y/n)?" );
  19. goOn = in.nextLine();
  20. goOn = in.nextLine(); //doesn't work in there's only one don't know why.
  21. largest = 0;
  22.  
  23. } while ( goOn.equals( "y" ) );
  24. }
  25. }
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
1
  #3
25 Days Ago
Originally Posted by masterofpuppets View Post
hi
for the first problem you could do something like this:

  1. public class Mymath {
  2.  
  3. public calculateFactor( int a, int b ) {
  4. if ( a < b && b % a == 0 )
  5. return true;
  6. elif ( a > b && a % b == 0 )
  7. return true;
  8. else
  9. return false;
  10. }
  11. }

and for the second part see if this is any helpful :

  1. import java.util.Scanner;
  2.  
  3. public class Test {
  4.  
  5. public static void main( String[] args ) {
  6. Scanner in = new Scanner( System.in );
  7. String goOn = "y";
  8. int n, largest = 0;
  9. do {
  10. System.out.println( "Input an integer number:" );
  11. n = in.nextInt();
  12.  
  13. for ( int i = 1; i < n; i++ )
  14. if ( n % i == 0 )
  15. if ( i > largest )
  16. largest = i;
  17. System.out.println( "Largest divisor: " + largest );
  18. System.out.println( "Another input (y/n)?" );
  19. goOn = in.nextLine();
  20. goOn = in.nextLine(); //doesn't work in there's only one don't know why.
  21. largest = 0;
  22.  
  23. } while ( goOn.equals( "y" ) );
  24. }
  25. }
Your first code will not compile and it is not java (elif)

The second is totally inefficient and shows no thinking at all to the real problem
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #4
25 Days Ago
For the second problem. You need the largest divisor? Then why start the loop from 1 and not from the number itself:
  1. int n; // read from input using Scanner;
  2. int maxDivisor = 1;
  3.  
  4. for (int i=n-1;i>0;i--) {
  5. if ( n%i == 0) {
  6. maxDivisor = i;
  7. break;
  8. }
  9. }
Also a more efficient way is to have the initial value to be the number dived by 2.

Think about it, the number 100 will not be divided by 99, 98, .. or any of those numbers. It will not be divided by 51, but it will be by 50.
For 93 (93.0/2.0 = 46.5) you can start checking from 46 because none of the numbers above its half can divide it.

In java:
93/2 = 46
93/2.0 = 46.5

So this would work:

  1. for (int i=n/2;i>0;i--) {
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #5
25 Days Ago
For the first problem the method would be:

  1. public int calculateFactor(int a, int b) {
  2. ...
  3. }

Inside you will do the checks described in the assignment. No explanation needed. The assignment tells you what to do and what to check. Surely you can understand by now how the '%' works.
Don't forget to check what happens if the 2 numbers are equal. You will still need to return one of the arguments, it doesn't matter which, since they are equal.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC