| | |
Input an integer number and display its largest factor
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 15
Reputation:
Solved Threads: 0
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.
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.
-1
#2 Nov 2nd, 2009
hi 
for the first problem you could do something like this:
and for the second part see if this is any helpful
:

for the first problem you could do something like this:
Java Syntax (Toggle Plain Text)
public class Mymath { public calculateFactor( int a, int b ) { if ( a < b && b % a == 0 ) return true; elif ( a > b && a % b == 0 ) return true; else return false; } }
and for the second part see if this is any helpful
: Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class Test { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); String goOn = "y"; int n, largest = 0; do { System.out.println( "Input an integer number:" ); n = in.nextInt(); for ( int i = 1; i < n; i++ ) if ( n % i == 0 ) if ( i > largest ) largest = i; System.out.println( "Largest divisor: " + largest ); System.out.println( "Another input (y/n)?" ); goOn = in.nextLine(); goOn = in.nextLine(); //doesn't work in there's only one don't know why. largest = 0; } while ( goOn.equals( "y" ) ); } }
1
#3 Nov 3rd, 2009
•
•
•
•
hi
for the first problem you could do something like this:
Java Syntax (Toggle Plain Text)
public class Mymath { public calculateFactor( int a, int b ) { if ( a < b && b % a == 0 ) return true; elif ( a > b && a % b == 0 ) return true; else return false; } }
and for the second part see if this is any helpful:
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class Test { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); String goOn = "y"; int n, largest = 0; do { System.out.println( "Input an integer number:" ); n = in.nextInt(); for ( int i = 1; i < n; i++ ) if ( n % i == 0 ) if ( i > largest ) largest = i; System.out.println( "Largest divisor: " + largest ); System.out.println( "Another input (y/n)?" ); goOn = in.nextLine(); goOn = in.nextLine(); //doesn't work in there's only one don't know why. largest = 0; } while ( goOn.equals( "y" ) ); } }
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
0
#4 Nov 3rd, 2009
For the second problem. You need the largest divisor? Then why start the loop from 1 and not from the number itself:
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)
int n; // read from input using Scanner; int maxDivisor = 1; for (int i=n-1;i>0;i--) { if ( n%i == 0) { maxDivisor = i; break; } }
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)
for (int i=n/2;i>0;i--) {
Check out my New Bike at my Public Profile at the "About Me" tab
0
#5 Nov 3rd, 2009
For the first problem the method would be:
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.
Java Syntax (Toggle Plain Text)
public int calculateFactor(int a, int b) { ... }
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
![]() |
Similar Threads
- program won't run (Java)
- the largest divisor (Java)
- Validate user input (Python)
- Factorial of any length (C++)
- Need help with do while loop (Java)
- help with programming (C++)
- Help with programming assignment (Python)
- Help Using For Loop To Display Ordered Numbers (C++)
Other Threads in the Java Forum
- Previous Thread: Infix to Postfix (HELP NEEDED)
- Next Thread: Pseduocode help - extracting parts of a real number
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






