WAP in Java to accept a number and display all its factors. Also show the dry run of your program.
Answers
Answer:
import java.util.Scanner;
public class Factors {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = scan.nextInt();
System.out.println("Factors of " + n + " are: ");
for(int i = n; i>=1; i--) {
if(n % i == 0) {
System.out.println(i);
}
}
}
}
Dry Run:
Enter a number:
8
Factors of 8 are:
8
4
2
1
Explanation:
This program displays the factors of the number input by the user.
Please mark me Brainliest if you are satisfied!
Answer:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:");
int n = sc.nextInt();
System.out.println("The factors of " + n + " are:");
for(int i = 1; i < n; i++){
if(n%i == 0){
System.out.print(i + " ");
}
}
System.out.print("and " + n);
}
}
Explanation: