VOOZH about

URL: https://www.geeksforgeeks.org/dsa/composite-number/

⇱ Composite Number Program - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Composite Number Program

Last Updated : 8 Oct, 2024

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. 


In below code optimized school method is discussed. 

Output: 

false
true

Time Complexity:- O(sqrt(n))

Space Complexity:-O(1)

Program on Composite Numbers


Reference :
https://en.wikipedia.org/wiki/Composite_number

Comment