943,940 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1110
  • Java RSS
Nov 2nd, 2009
-1

Input an integer number and display its largest factor

Expand Post »
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.
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
kulrik is offline Offline
15 posts
since Oct 2009
Nov 2nd, 2009
-1
Re: Input an integer number and display its largest factor
hi
for the first problem you could do something like this:

Java Syntax (Toggle Plain Text)
  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 :

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 3rd, 2009
1
Re: Input an integer number and display its largest factor
hi
for the first problem you could do something like this:

Java Syntax (Toggle Plain Text)
  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 :

Java Syntax (Toggle Plain Text)
  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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 3rd, 2009
0
Re: Input an integer number and display its largest factor
For the second problem. You need the largest divisor? Then why start the loop from 1 and not from the number itself:
Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  1. for (int i=n/2;i>0;i--) {
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 3rd, 2009
0
Re: Input an integer number and display its largest factor
For the first problem the method would be:

Java Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Infix to Postfix (HELP NEEDED)
Next Thread in Java Forum Timeline: Pseduocode help - extracting parts of a real number





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC