Composite Number in JAVA
Composite Number in JAVA
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 below :-
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
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
For Complete Explanation watch this video :-
Follow me on Instagram
All the Best :)
Keep Learning :)
I think the code on the below site is much easier to understand than this one.
ReplyDeletehttps://solved-programs.blogspot.com/2021/04/composite-number-in-java.html
ertg3rt
ReplyDelete