Composite Number In Java Trick 2024 | topperbhai.com

 Composite Number  In Java  Trick

Composite Number In Java
 

What Is   a Composite Number?

A number which is not a prime number is called a Composite Number. In other words, a number which has factors other than one and the number itself.

 All non-prime numbers are composite numbers for e.g. 4,6,8,9,12,14 etc.


In this tutorial, we will write a program to check whether a number is composite  number or not in java.


 
First, we will write the composite  number in java algorithm, then we will write the complete java code of this program.


Composite Number in Java Algorithm:-

Step 1:  Initialize a number (n).

Step 2:  Use loop to find factors of number (n).

Step 3:  For every factor, increase the value of (count) by 1.

Step 4:  If (count) is greater than 2.

Step 5:  Print "Number is Composite" else not.

 

 Composite Number In Java

 import java.util.*;  
 class Composite_Numbers  
 {  
   public static void main()  
   {  
     Scanner sc=new Scanner(System.in);  
     int n,i,count=0;  
     System.out.println("Enter any Number");  
     n=sc.nextInt();  
     for(i=1;i<=n;i++)  
     {  
       if(n%i==0)  
       {  
         count++;  
       }  
     }  
     if(count>2)  
     {  
       System.out.println("It is a Composite Number");  
     }  
     else  
     {  
       System.out.println("It is not a Composite Number");  
     }  
   }  
 }  
OUTPUT: 
 Enter any Number  
 5  
 It is not a Composite Number  

Post a Comment

0 Comments