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

JavaScript Prime, Fibonacci, Factorial Codes

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)
5 views3 pages

JavaScript Prime, Fibonacci, Factorial Codes

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

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 <= [Link](number); i += 2) {

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

return true;

// Example usage:

let num = 29;

if (isPrime(num)) {

[Link](num + " is a prime number.");

} else {

[Link](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 || ![Link](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 ([Link] < 10) {

let isPrime = true;

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

if (num % i === 0) {

isPrime = false;

break;

if (isPrime) {

primes[[Link]] = num; // Instead of [Link](num)

num++;

[Link](primes);

You might also like