Java – Factors Programs

Program to print factors of given number

import java.util.Scanner;
class Code
{
            public static void main(String[] args)
            {
                        Scanner sc = new Scanner(System.in);
                        System.out.print(“Enter n value : “);
                        int n = sc.nextInt();
                        for (int i=1 ; i<=n ; i++)
                        {
                                    if(n%i==0)
                                    {
                                                System.out.println(i + ” is a factor”);
                                    }
                        }
            }
}

Program to count factors of given number

import java.util.Scanner;
class Code
{
            public static void main(String[] args)
            {
                        Scanner sc = new Scanner(System.in);
                        System.out.print(“Enter n value : “);
                        int n = sc.nextInt();
                        int count=0;
                        for (int i=1 ; i<=n ; i++)
                        {
                                    if(n%i==0)
                                    {
                                                count++;
                                    }
                        }
                        System.out.println(“Factors count is : ” + count);
            }
}

Program to find the sum of factors of given number

import java.util.Scanner;
class Code
{
            public static void main(String[] args)
            {
                        Scanner sc = new Scanner(System.in);
                        System.out.print(“Enter n value : “);
                        int n = sc.nextInt();
                        int sum=0;
                        for (int i=1 ; i<=n ; i++)
                        {
                                    if(n%i==0)
                                    {
                                                sum=sum+i;
                                    }
                        }
                        System.out.println(“Sum of Factors : ” + sum);
            }
}
Scroll to Top