# Composite Number

• Difficulty Level : Hard
• Last Updated : 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:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

```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.

In below code optimized school method is discussed.

## C++

 `// A optimized school method based C++ program to check``// if a number is composite.``#include ``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 function``int` `main()``{``    ``isComposite(11)?  cout << ``" true\n"``: cout << ``" false\n"``;``    ``isComposite(15)?  cout << ``" true\n"``: cout << ``" false\n"``;``    ``return` `0;``}`

## Java

 `/// 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`

## Python 3

 `# 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.`

## C#

 `// 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

 ``

## Javascript

 ``

Output:

```false
true```

Program on Composite Numbers

Reference :
https://en.wikipedia.org/wiki/Composite_number
This article is contributed by Ajay Puri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.