Factor of the given number in Java


The numbers that are completely divisible by the given number (it means the remainder should be 0) called as factors of a given number using for the for loop and if condition statement. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. In for loop, a loop variable is used to control the loop. The if statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. The if statement is written with the if keyword, followed by a condition in parentheses, with the code to be executed in between curly brackets.
Example :
     limit = 5
    5 1 => 5%1 = 0
    5 2 => 5%2 = 0
    5 3 => 5%3 = 1
    5 4 => 5%4 = 2
    5 5 => 5%5 = 0

Source Code

import java.util.Scanner;
 
public class factor {
    //Write a program to find the factor of the given number.
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter The Number : ");
        int n = in.nextInt();
        for(int i=1;i<=n;i++)
        {
            if(n%i==0){
                System.out.println(i);
            }
        }
    }
}
 
 

Output

Enter The Number : 10
1
2
5
10
To download raw file Click Here

Basic Programs