Spirituality average of first 10 prime numbers using javascript

average of first 10 prime numbers using javascript

javascript
Copy code
// Function to check if a number is prime
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}

let count = 0; // Counter for number of primes found
let sum = 0; // Running total of prime numbers
let i = 2; // Start searching for primes from 2

while (count < 10) {
if (isPrime(i)) {
count++; // Increment counter
sum += i; // Add prime to running total
}
i++; // Move on to next number
}

// Calculate average of the first 10 primes
const average = sum / count;

console.log(`The average of the first 10 prime numbers is ${average}.`);
When you run this program, it will output the following result:

csharp
Copy code
The average of the first 10 prime numbers is 12.9.
Note that the average of the first 10 prime numbers is not an integer, so the result is a floating-point number.


There is no ads to display, Please add some

Related Post