write a program in java to enter a number and display all factors of the entered number. sample input: 18 sample output 1,2,3,6,9,18
Answers
Answered by
10
Answer:
package Programmer;
import java.util.*;
public class Factor{
public static void main (String ar []){
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
for(int i=1;i<=n;i++){
if(n%i==0)
System.out.print(i+" ");
}
}
}
___
Variable Description:-
- n:- to accept the number
- I:- loop Variable used to run a loop from 1 to n
___
Explaination:-
- The program accepts the number from the user
- Then it runs i loop from 1 to n
- then it checks if n is completely divisible by I or not
- If yes, it is a factor and prints i
- else not a factor
- Yay! your required program is working!
___
•output Attached
Attachments:
Similar questions