menu
QAmmunity.org
Login
Register
My account
Edit my Profile
Private messages
My favorites
Register
Ask a Question
Questions
Unanswered
Tags
Categories
Ask a Question
Design an algorithm for finding all the factors of a positive integer. For example, in the case of the integer 12, your algorithm should report the values 1, 2, 3, 4, 6, and 12
asked
Aug 2, 2018
87.7k
views
2
votes
Design an algorithm for finding all the factors of a positive integer. For example, in the case of the integer 12, your algorithm should report the values 1, 2, 3, 4, 6, and 12
Computers and Technology
college
Tuzzer
asked
by
Tuzzer
9.7k
points
answer
comment
share this
share
0 Comments
Please
log in
or
register
to add a comment.
Please
log in
or
register
to answer this question.
1
Answer
4
votes
As I say to every one requesting programming help on these forums. If you don't specify a language, then it just means you'll have to translate the answer that is given to you, to a different language. Here's what you want in Java:
import java.util.InputMismatchException
;
import java.util.Scanner
;
public class Test {
public static void main(String[] args) {
int number = numInput()
;
printFactors(Math.abs(number))
;
}
private static int numInput() {
Scanner sc = new Scanner(System.in)
;
System.out.print("Enter number: ")
;
int input
;
while (true) {
try {
input = sc.nextInt()
;
break;
} catch (InputMismatchException e) {
System.out.println("Not a number.")
;
}
}
return input
;
}
private static void printFactors(int number) {
if (number == 0) {
System.out.println("Infinite factors.")
;
} else {
for (int i = 1; i <= number; i++) {
for (int j = 1; j <= number; j++) {
if (i * j == number) {
System.out.print(i + ", ")
;
}
}
}
}
}
}
Humungs
answered
Aug 9, 2018
by
Humungs
8.4k
points
ask related question
comment
share this
0 Comments
Please
log in
or
register
to add a comment.
Ask a Question
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.
9.5m
questions
12.2m
answers
Other Questions
“What does it mean when we “rework” copyrighted material?”
The book shows how to add and subtract binary and decimal numbers. However, other numbering systems are also very popular when dealing with computers. The octal (base 8) numbering system is one of these.
Seven basic internal components found in a computer tower
Please help me ! All you do is just put it it all in your own words ! Please this is for my reported card!i don't know how to put it in my own words because my English is not that good!
describe an advance in technology that makes life more enjoyable. what discoveries contribute to this technology?
Twitter
WhatsApp
Facebook
Reddit
LinkedIn
Email
Link Copied!
Copy
Search QAmmunity.org