Write a program in Java to accept a number from user and display it's factor
Answers
Answered by
2
package SimplerPrograms;
import java.util.Scanner;
public class FactorsOfNumberUsingFor {
private static Scanner sc;
public static void main(String[] args) {
int Number, i;
sc = new Scanner(System.in);
System.out.println("Please Enter any number to Find Factors: ");
Number = sc.nextInt();
for(i = 1; i <= Number; i++) {
if(Number%i == 0) {
System.out.format(" %d ", i);
}
}
}
}
import java.util.Scanner;
public class FactorsOfNumberUsingFor {
private static Scanner sc;
public static void main(String[] args) {
int Number, i;
sc = new Scanner(System.in);
System.out.println("Please Enter any number to Find Factors: ");
Number = sc.nextInt();
for(i = 1; i <= Number; i++) {
if(Number%i == 0) {
System.out.format(" %d ", i);
}
}
}
}
Answered by
0
Hope it helps ^_^
I have also added all the possible outputs
import java.util.Scanner;
public class FactorTeller{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int i, num;
System.out.print("Enter a number in order to know its factors: ");
num = sc.nextInt();
System.out.println("The factors of the given number are as follows: ");
for (i = 1; i <= num; i++){
if(num % i == 0){
System.out.println(i);
}
}
}
}
Outputs:
Enter a number in order to know its factors: 20
The factors of the given number are as follows:
1
2
4
5
10
20
Similar questions
Political Science,
7 months ago
Math,
7 months ago
Computer Science,
1 year ago
Social Sciences,
1 year ago
Physics,
1 year ago
Math,
1 year ago
Science,
1 year ago