Java Program to read a number then print the factors and print prime factors

User enter nth place of number then Java program read this number then print the factors of a number and print prime factors of this number.

Code Here:
import java.util.*;
class primefactor
{
 public static void main(String s[])
 {
  int i,n,j=0,count=0 ;
  Scanner sc=new Scanner(System.in);
  int a[]=new int[10];
  System.out.print("Enter the value of nth no. of place : ");
  n=sc.nextInt();
  System.out.println("The factors are:");
  for(i=1;i<=n;i++)
  {
   if(n%i==0)
   {
    a[j++]=i;
   }
  }
  for(i=0;i<j;i++)
  System.out.println(a[i]+"\t");
  System.out.println("The prime factors are:");
  for(i=0;i<j;i++)
  {
  count=0;
  for(int k=1;k<=a[i];k++)
  {
   if(a[i]%k==0)
   count++;
  }
  if(count==2)System.out.print("\t"+a[i]);
  }
 }
}

Output:

Enter the value of nth no. of place: 10
The factors are:
1 2 5 10
The prime factors are :
2 5

Post a Comment

0 Comments