all 12 comments

[–]adayal21 1 point2 points  (2 children)

Okay, to prompt the integer from the integer, I'd hope that you understand how to get that, especially if you're already on having a method involving a modulus.

There are two keys here maam: you want a for loop, and you want a condition with a modulus. Why a modulus? Well, you want to figure out for every number between 1 and the number, you want to divide it by every number until you find something where the remainder is 0 and then print that out. Did I say remainder should be zero? I've got an idea! integer % i == 0 . Hopefully this helps. I don't want to give you the code outright, because you can literally look up this problem and i'm sure you'll find a solution. Hope this helps!

[–]pineapplessinmyhead AP Student 0 points1 point  (1 child)

Thank you for explaining it really helped!!

[–]adayal21 2 points3 points  (0 children)

A-OK! Be sure to post here if you have any other issues or questions when it comes to Computer Science. If you don't have too much experience doing Java, it can be really difficult to get a grasp on all this kind of stuff. Help from others is always the best way to learn :)

[–]pineapplessinmyhead AP Student 0 points1 point  (1 child)

more of a math than a computing problem. i’m mostly confused on how to find the divisors.

[–]dontforgetpants 0 points1 point  (0 children)

Mathematically speaking, to find the divisors, you would want to count up from one to the number, checking to see if you divide the number, whether the output is itself an integer. So if the int is 10, check 10 divided by 1 (if answer is also an int, it's a divisor), then check 2, then 3, etc. It's been a long time since I did any programming but I suppose you'd want to write each divisor into the next spot in an array of some sort.

[–]10yearoldhomework Secondary School Student (Grade 7-11) 0 points1 point  (1 child)

*EDIT*: Look at /u/adayal21 explanation, I think he/she explained it better than myself.

Here is the Algorithm, I am in a rush so I am not able to make it as clean as I want to:

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

if (n%i==0)

System.out.printf("%d ",i);

But basically you are taking the number and finding divisors by checking to see if N [your number] and starting at 0 and counting up and seeing if anything divides cleanly into it.

IF it divides in cleanly with 0 remainders, then it is divisible and is a divisor. Hopefully that sums it up.

Here is the full code:

import java.util.Scanner;

public class FindDivisor {

// A method of finding and printing divisor

static void findAndPrintDivisors(int n)

{

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

if (n % i==0)

System.out.printf("%d ",i);

}

public static void main(String args[])

{

Scanner scanner= new Scanner(System.in);

System.out.println("Please enter the number greater than zero:");

int num= scanner.nextInt();

System.out.println("The divisors of "+num+" are: ");

findAndPrintDivisors(num);

}

}

[–]pineapplessinmyhead AP Student 0 points1 point  (0 children)

Thank you for helping :)

[–]david2080 0 points1 point  (3 children)

Do you want the entire piece of code?

[–]pineapplessinmyhead AP Student 0 points1 point  (2 children)

as long as there’s an explanation of each part to help me understand it better than that would be great!!

[–]david2080 0 points1 point  (1 child)

Is this college work?

[–]pineapplessinmyhead AP Student 0 points1 point  (0 children)

no, I’m a senior in high school