JavaNewGirl - 6 months ago 16

Java Question

I have written the program however I have two problems :

- I do not want the counter i to go from 1 to x because then it would try the divisibility test for every number less than the actual user input. I need it to start i from 2 and go until 12 so that you try the test for 2-12 only.
- The divisibility test is correct and it works for every number but that's not whats asked in the program description. I need to implement the mentioned algorithms for each divisibility test. (which i have researched but am not sure how to do it) this simplified program that i have makes more sense - just a place to start would be helpful

(NOTE: cannot use % modulus operator)

Div by 2 The last digit is even (0,2,4,6,8) Ex: 128 is 129 is not

Div by 3 The sum of the digits is divisible by 3 Ex:381 (3+8+1=12, and 12÷3 = 4) Yes 217 (2+1+7=10, and 10÷3 = 3 1/3) No

Div by 4 The last 2 digits are divisible by 4 Ex: 1312 is (12÷4=3) 7019 is not 5 The last digit is 0 or 5 175 is 809 is not

Div by 6 (Note: You may do six as a separate user-defined function or use 2 and 3’s functions) The number is divisible by both 2 and 3 Ex: 114 (it is even, and 1+1+4=6 and 6÷3 = 2) Yes 308 (it is even, but 3+0+8=11 and 11÷3 = 3 2/3) No

There is more but again - just a place to start or link that has some useful info would be appreciated. Thank you

`import java.io.PrintStream;`

import java.util.Scanner;

public class rwsFinalExam

{

public static void main(String [] args)

{

Scanner scanner = new Scanner( System.in );

//allows input from concole

PrintStream out = System.out;

//assigning System.out to PrintStream

out.print( "Input a valid whole number: " );

//ouput directions for end user to enter a whole number

String input = scanner.next(); //holds end user input

int number; //data type and variable

try

{

number = Integer.parseInt(input);

//assinging value to number

//integer.parseInt method converts string to int

}

catch (Exception e)

{

out.println(input + " is not a valid number");

return;

}

if (number < 0)

{

out.println(number + " is not a valid number");

return;

}

printDivisors(number);

}

private static void printDivisors(int x)

{

PrintStream out = System.out;

for (int i=1; i<x; i++)

{

if (isDivisibleBy(x, i)) //checking divisibility

{

out.println(x + " is divisible by " + i);

//output when value is divisible

}

else

{

out.println(x + " is not divisible by " + i);

//output when value not divisible

}//end if else

}//end for

}//end private static

private static Boolean isDivisibleBy(int x, int divisor)

{

while (x > 0)

{

x -= divisor;

if (x == 0)

{

return true;

}

}

return false;

}//end

}//end class rwsFinalExam

Answer

The first thing you should do is get the digits in an array.

```
private static Boolean isDivisibleBy(int x, int divisor)
{
char[]b=String.valueOf(x).toCharArray();
//Store all your digits as characters
int[]no= new int[b.length];
for (int i=0;i<b.length;i++)
no[i]=Integer.parseInt(String.valueOf(b[i]));
//Now no contains all your integers as an array
if(divisor==2)
return isDivisibleBy2(no);
else if(divisor==3)
return isDivisibleBy3(no);
else if(divisor==4)
return isDivisibleBy4(no);
//...................
else return false;
}
```

Then for each test write a method like so for 2:

```
private static Boolean isDivisibleBy2(int[] x){
int lastDigit=x[x.length-1];
if(lastDigit==0 || lastDigit==2 || lastDigit==4 || lastDigit==6 || lastDigit==8)
return true;
else
return false;
}
```

Anyway you'll have to write similar code for each test.