JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to find Composite Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, identifying and working with different types of numbers is a fundamental skill.
A composite number is one that has more than two positive divisors, excluding 1 and itself. In this tutorial, we'll explore a javascript program that efficiently determines whether a given number is composite or not.
π Example
Let's dive into the javascript code that checks whether a number is composite or not.
// Function to check if a number is composite
function isCompositeNumber(number) {
if (number <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return true; // Found a divisor other than 1 and itself
}
}
return false; // Number is not divisible by any number other than 1 and itself
}
// Driver program
// Replace this value with your desired number
const inputNumber = 12;
// Check if the input number is composite
if (isCompositeNumber(inputNumber)) {
console.log(`${inputNumber} is a composite number.`);
} else {
console.log(`${inputNumber} is not a composite number.`);
}
π» Testing the Program
To test the program with a different number, simply replace the value of inputNumber in the code.
12 is a composite number.
Run the script to see whether the input number is a composite number.
π§ How the Program Works
- The program defines a function isCompositeNumber that takes a number as input and returns true if the number is composite and false otherwise.
- Inside the function, it first checks if the number is less than or equal to 1. If so, it returns false since 1 and numbers less than 1 are not considered composite.
- It then iterates from 2 to the square root of the input number. For each iteration, it checks if the number is divisible by the current value of i. If it is, the function returns true, indicating that the number has a divisor other than 1 and itself.
- If no such divisor is found during the iteration, the function returns false, indicating that the number is not a composite number.
π Between the Given Range
Let's take a look at the JavaScript code that identifies composite numbers in the specified range.
// Function to check if a number is composite
function isComposite(number) {
if (number <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return true; // Number has a divisor other than 1 and itself
}
}
return false; // Number is not composite
}
// Check for composite numbers in the range 1 to 10
console.log("Composite numbers in the range 1 to 10:");
for (let i = 1; i <= 10; i++) {
if (isComposite(i)) {
console.log(i);
}
}
π» Testing the Program
Composite numbers in the range 1 to 10 are: 4 6 8 9 10
Run the script to see the composite numbers in the range from 1 to 10.
π§ How the Program Works
- The program defines a function isComposite that checks if a given number is composite.
- Inside the function, it checks for divisors from 2 up to the square root of the number.
- If a divisor is found, the number is composite; otherwise, it is not.
- The main section then checks for composite numbers in the range from 1 to 10 using the isComposite function.
π§ Understanding the Concept of Composite Numbers
Composite numbers are integers greater than 1 that have divisors other than 1 and themselves.
For example, the number 12 is composite because it can be formed by multiplying 2 and 6 or 3 and 4.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing optimizations for larger numbers, such as checking divisors up to the square root of the number for improved efficiency.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to find Composite Number), please comment here. I will help you immediately.