0% found this document useful (0 votes)
13 views3 pages

Javascript

Uploaded by

Raj Savaliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Javascript

Uploaded by

Raj Savaliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a JavaScript code to find whether given number is prime or not.

function isPrime(number) {

if (number <= 1) return false;

if (number === 2) return true;

if (number % 2 === 0) return false;

for (let i = 3; i <= Math.sqrt(number); i += 2) {

if (number % i === 0) return false;

return true;

// Example usage:

let num = 29;

if (isPrime(num)) {

console.log(num + " is a prime number.");

} else {

console.log(num + " is not a prime number.");

Write a Java Script code to display Fibonacci Series of given number. Number should be
entered by user through text box.

let n = prompt("Enter number of terms:");


n = number (n);

let a = 0, b = 1, next;

let result = "";

for (let i = 1; i <= n; i++) {

result += a + " ";

next = a + b;

a = b;

b = next;

alert("Fibonacci Series:\n" + result);

Factorial program

let num = Number(prompt("Enter a number to find its factorial:"));

if (isNaN(num) || num < 0 || !Number.isInteger(num)) {

alert("Please enter a valid non-negative integer.");

} else {

let factorial = 1;

for (let i = 1; i <= num; i++) {

factorial *= i;

alert("Factorial of " + num + " is " + factorial);

}
Java Script for find first 10 prime numbers

let primes = [];

let num = 2;

while (primes.length < 10) {

let isPrime = true;

for (let i = 2; i < num; i++) {

if (num % i === 0) {

isPrime = false;

break;

if (isPrime) {

primes[primes.length] = num; // Instead of primes.push(num)

num++;

console.log(primes);

You might also like