-3

I have written the program however I have two problems :

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

  2. 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
Jongware
  • 22,200
  • 8
  • 54
  • 100
JavaNewGirl
  • 75
  • 1
  • 7
  • I'm sorry your question is a little unclear why can't you just do for(int i=2;i<12;i++) – cjds Dec 10 '12 at 04:12
  • Why up to...12? Suppose I enter 1024. Its divisors go *way* beyond 12. Also, as a heads-up, you can use up to half the actual number to check for divisors, since there's a mathematical property that guarantees that you'll have them all covered by the time you get to that value. – Makoto Dec 10 '12 at 04:12
  • @Makoto I think for prime checking manually what we do is to check divisors upto 13. I would like to clarify. – Extreme Coders Dec 10 '12 at 04:14
  • 2
    "I need to implement the mentioned algorithm" which algorithm? – cjds Dec 10 '12 at 04:14
  • Edit your question and add the tests in it – cjds Dec 10 '12 at 04:30
  • Thank you Carl - I put a little more info in my question but not all as I don't want anyone to think I need it written for me. I have the program written in a longer form also but i am not sure if it is correct. - this program has the correct output – JavaNewGirl Dec 10 '12 at 04:38

1 Answers1

0

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.

cjds
  • 8,268
  • 10
  • 49
  • 84
  • You can try this method. But all I've given you is a way to parse the input so you can manipulate the digits easily. Please figure out the divisibility algorithms on your own but if you have any problem ask – cjds Dec 10 '12 at 04:44
  • I know this question is closed, however this is a very bad solution. The questioner already has a method for working out divisibility for **any** two numbers. The _only_ change needed is `for (int i=1; i `for (int i=2; i<13; i++)`.You do not need a method for every divisibility test. You do not need to convert the integers into a char or int array. – AnnanFay Dec 11 '12 at 22:58
  • Apologies, I misread the original question. It seems a new method is needed for every test, though I find this mind boggling. – AnnanFay Dec 11 '12 at 23:02
  • @Annan yes it is mind boggling - that is why i was at a loss - it was like a huge elementary school word problem that include a bunch of busy work. it ended up with 545 lines of code.... crazy when it could be done in 75. – JavaNewGirl Dec 12 '12 at 23:05
  • @JavaNewGirl I think the idea would be more to teach you the basics of handling the maths for each method. And each method is pretty easy when you go to see. You only have to do it for 2,3,5,7 ..i.e. the prime numbers. The rest are just divisions on divisions so it shouldn't really take that many lines – cjds Dec 13 '12 at 02:46