Solve any Factor Program in 10 minutes
- Categories Java, Iterative Statements, For & While
Watch our video on Solve any Factor Program in 10 minutes in Java – Click Here
1 . Find if a number is prime number. prime number is a number divisible by 1 and itself e.g 5 is a primt number as it has factor only 1,5
import java.util.*; public class PrimeNum { public static void main(String args[]) { Scanner sc =new Scanner(System.in); int count=0; System.out.println("enter a number"); int n= sc.nextInt( ); for(int i=1;i<=n;i++) { if(n%i==0) count++; } if(count==2) System.out.println("Yes"); else System.out.println("No"); } }
2 . Find if a number is a composite number. Composite number is a number which has more than one factor(excluding 1 and itself) e.g 8=2, 4=2 factors
import java.util.*; public class compositeNo { public static void main(String args[]) { Scanner sc =new Scanner(System.in); int count=0; System.out.println("enter a number"); int n= sc.nextInt( ); for(int i=1;i<=n;i++) { if(n%i==0) count++; } if(count>3) System.out.println("Yes"); else System.out.println("No"); } }
3 . Find if a number is a perfect number. A perfect number is number which is equal to sum of its divisor or factor except itself e.g. 6=1+2+3
import java.util.*; public class perfectNo { public static void main(String args[]) { Scanner sc =new Scanner(System.in); int sum=0; System.out.println("enter a number"); int n= sc.nextInt( ); for(int i=1;i<n;i++) { if(n%i==0) sum= sum+i; } if(sum==n) System.out.println("Yes"); else System.out.println("No"); } }
4 . Find if a number is an Abundant number. Here sum of factor is greater then the number. itself e.g. 12 factor 1,2,3,4,6=16>12
import java.util.*;
public class AbundantNo
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
int sum=0;
System.out.println("enter a number");
int n= sc.nextInt( );
for(int i=1;i<n;i++)
{
if(n%i==0)
sum= sum+i;
}
if(sum>n)
System.out.println("Yes");
else
System.out.println("No");
}
}
5 . Find if number is a Deficient number. Here sum of factor is less than the no itself. e.g. 21 factor 1,3,7=11<21
import java.util.*;
public class DeficientNo
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
int sum=0;
System.out.println("enter a number");
int n= sc.nextInt( );
for(int i=1;i<n;i++)
{
if(n%i==0)
sum= sum+i;
}
if(sum<n)
System.out.println("Yes");
else
System.out.println("No");
}
}
6 . Find if number is a Pronic number. Pronic No is the product of two consecutive integers, n(n+1). e.g. 56=7×8
import java.util.*;
public class PronicNo
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
int fact=0;
System.out.println("enter a number");
int n= sc.nextInt( );
for(int i=1;i<n;i++)
{
if(i*(i+1)==n)
fact=i;
}
if(fact!=0)
System.out.println("Yes");
else
System.out.println("No");
}
}