Composite Number

08 Mar, 2021
A composite number is a positive integer that is not prime. In other words, it has a positive divisor other than one or itself. First few composite numbers are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, .........  
Every integer greater than one is either a prime number or a composite number.
The number one is a unit – it is neither prime nor composite.
How to check if a given number is a composite number or not? Examples:  
Input : n = 21
Output: Yes
The number is a composite number!

Input : n = 11
Output : No
 
The idea is simple, we can use any of the below methods used for prime checking. We just need to change return statements. Return true is changed to return false and vice versa.  
Primality Test | Set 1 (Introduction and School Method)
Primality Test | Set 2 (Fermat Method)
Primality Test | Set 3 (Miller–Rabin)
In below code optimized school method is discussed.  
C++
Java
Python 3
C#
PHP
Javascript
// A optimized school method based C++ program to check// if a number is composite.#include <bits/stdc++.h>using namespace std;  bool isComposite(int n){    // Corner cases    if (n <= 1)  return false;    if (n <= 3)  return false;      // This is checked so that we can skip    // middle five numbers in below loop    if (n%2 == 0 || n%3 == 0) return true;      for (int i=5; i*i<=n; i=i+6)        if (n%i == 0 || n%(i+2) == 0)           return true;      return false;}    // Driver Program to test above functionint main(){    isComposite(11)?  cout << " true\n": cout << " false\n";    isComposite(15)?  cout << " true\n": cout << " false\n";    return 0;}
/// An optimized method based Java// program to check if a number// is Composite or not.import java.io.*; class Composite{    static boolean isComposite(int n)    {        // Corner cases        if (n <= 1)        System.out.println("False");                 if (n <= 3)        System.out.println("False");         // This is checked so that we can skip        // middle five numbers in below loop        if (n % 2 == 0 || n % 3 == 0) return true;         for (int i = 5; i * i <= n; i = i + 6)            if (n % i == 0 || n % (i + 2) == 0)            return true;         return false;    }     // Driver Program to test above function    public static void main(String args[])    {        System.out.println(isComposite(11) ?                       "true" : "false");                                System.out.println(isComposite(15) ?                       "true" : "false");    }} // This code is contributed by Anshika Goyal
# A optimized school method based Python program to check# if a number is composite. def isComposite(n):     # Corner cases    if (n <= 1):        return False    if (n <= 3):        return False     # This is checked so that we can skip    # middle five numbers in below loop    if (n % 2 == 0 or n % 3 == 0):        return True    i = 5    while(i * i <= n):                 if (n % i == 0 or n % (i + 2) == 0):            return True        i = i + 6             return False # Driver Program to test above function print("true") if(isComposite(11)) else print("false")print("true") if(isComposite(15)) else print("false")# This code is contributed by Anant Agarwal.
// A optimized school method based C# program// to check if a number is composite.using System; namespace Composite{public class GFG{                         public static bool isComposite(int n)    {             // Corner cases    if (n <= 1) return false;    if (n <= 3) return false;     // This is checked so that we can skip    // middle five numbers in below loop    if (n % 2 == 0 || n % 3 == 0) return true;     for (int i = 5; i * i <= n; i = i + 6)             if (n % i == 0 || n % (i + 2) == 0)        return true;     return false;    }      // Driver Code    public static void Main()    {             if(isComposite(11)) Console.WriteLine("true");    else Console.WriteLine("false");         if(isComposite(15)) Console.WriteLine("true");    else Console.WriteLine("false");    } }}  // This code is contributed by Sam007
<?php// A optimized school// method based PHP// program to check// if a number is composite. function isComposite($n){         // Corner cases    if ($n <= 1)        return false;    if ($n <= 3)        return false;     // This is checked so    // that we can skip    // middle five numbers    // in below loop    if ($n%2 == 0 || $n % 3 == 0)        return true;     for ($i = 5; $i * $i <= $n;                   $i = $i + 6)        if ($n % $i == 0 || $n % ($i + 2) == 0)        return true;     return false;}     // Driver Code    if(isComposite(11))        echo "true";        else        echo "false";        echo"\n";    if(isComposite(15))        echo "true";        else        echo "false";        echo"\n";     // This code is contributed by Ajit.?>
<script> // A optimized school method based Javascript program to check// if a number is composite. function isComposite(n){    // Corner cases    if (n <= 1) return false;    if (n <= 3) return false;     // This is checked so that we can skip    // middle five numbers in below loop    if (n%2 == 0 || n%3 == 0) return true;     for (let i=5; i*i<=n; i=i+6)        if (n%i == 0 || n%(i+2) == 0)        return true;     return false;}  // Driver Program to test above function      isComposite(11)? document.write(" true" + "<br>"): document.write(" false" + "<br>");    isComposite(15)? document.write(" true" + "<br>"): document.write(" false" + "<br>");       // This code is contributed by Mayank Tyagi </script>
Output: 
false
true
  Program on Composite Numbers  
Find a range of composite numbers of given length
Generate a list of n consecutive composite numbers (An interesting method)
Sum and product of k smallest and k largest composite numbers in the array
Product of all the Composite Numbers in an array
Count and Sum of composite elements in an array
Split n into maximum composite numbers
Reference : https://en.wikipedia.org/wiki/Composite_numberThis article is contributed by Ajay Puri. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Akanksha_Rai
mayanktyagi1709
Prime Number
series
Mathematical
School Programming
Mathematical
Prime Number
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java