Composite Number
A Composite Number is a positive integer that has at least one positive divisor other than one or the number itself.
Example : 4,6,8 etc are composite number.
Algorithm and Program below :-
Algorithm :-
Get the number to check for Composite Number
Hold the number in another variable
Initialize a variable c=0 (let)
Using loop find its factors
For every factor increase the value of c by 1
If c is greater than one print "Composite Number"
If not print "Not a Composite Number"
Program :-
import java.io.*; import java.util.*; class Composite { public static void main(String args[]) { Scanner in= new Scanner(System.in); int n,p,i,c=0; System.out.println("Enter Number"); n= in.nextInt(); // Input number from user p=n; // store the entered number in "p" variable for(i=2;i<n;i++) { if(n%i==0) // checking factors of the number { c++; } } if(c>1) // if factors are greater than 1 { System.out.println("It is Composite Number : "+p); } else { System.out.println("It is not Composite Number : "+p); } } // end of main method } // end of class
Note : The program is written in the easiest way possible so try understand it. If you face any problem in understanding this program than feel free to contact/comment.
All the Best :)
Keep Learning :)
Commenti