> Java code to find the divisors of a given number? such as: entering six, and getting 1,2,3,6 as output?

Java code to find the divisors of a given number? such as: entering six, and getting 1,2,3,6 as output?

Posted at: 2014-12-18 
Im trying to do this with loops and modulus, but i'm not sure how i should tackle the logic part. and help would be super useful, Thanks :)

Here you go:

import java.util.Scanner;

public class Divisors {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter number: ");

int n = sc.nextInt();;

System.out.print(1);

for (int i = 2; i <= n; i++) {

if (n % i == 0) {

System.out.print("," + i);

}

}

System.out.println();

}

}

//n being the number you want to find out the divisible answers.

import java.lang.Math.*;

public class HelloWorld{

public static void main(String[] args){

double n = 6.0;

double a;

int i;

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

a = (n/i);

if (a == Math.ceil(a)) {

System.out.println(a);

}

}

}

}

Im trying to do this with loops and modulus, but i'm not sure how i should tackle the logic part. and help would be super useful, Thanks :)