Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Created May 29, 2021 15:13
Show Gist options
  • Save bijay-shrestha/309545651a0ed7be6e8898f335a41ae3 to your computer and use it in GitHub Desktop.
Save bijay-shrestha/309545651a0ed7be6e8898f335a41ae3 to your computer and use it in GitHub Desktop.
package com.hawa.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FactorsOfNumber {
public static void main(String[] args) {
int num = 10;
log.info("The factors of {} are {}", num, getFactorsOfNumber(num));
}
static int[] getFactorsOfNumber(int n) {
int x = 0;
int[] arr = new int[countFactorsOfNumber(n)];
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
arr[x] = i;
x++;
}
}
return arr;
}
static int countFactorsOfNumber(int num) {
int count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
count++;
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment