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

## All 4 Replies

hi :)
for the first problem you could do something like this:

``````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 :):

``````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" ) );
}
}``````

hi :)
for the first problem you could do something like this:

``````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 :):

``````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" ) );
}
}``````

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

For the second problem. You need the largest divisor? Then why start the loop from 1 and not from the number itself:

``````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;
}
}``````

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:

``for (int i=n/2;i>0;i--) {``

For the first problem the method would be:

``````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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.