Javascript Program
program to swap two variables without temp variable.
let a = 5;
let b = 10;
// Swapping without a temporary variable using destructuring
[a, b] = [b, a];
console.log("After swapping:");
console.log("a =", a);
console.log("b =", b);
Leap Year
function isLeapYear(year) {
// Check if the year is divisible by 4
if (year % 4 === 0) {
// Check if it's divisible by 100
if (year % 100 === 0) {
// If divisible by 100, it must also be divisible by 400
if (year % 400 === 0) {
return true; // It's a leap year
} else {
return false; // Not a leap year
} else {
return true; // It's a leap year
} else {
return false; // Not a leap year
}
// Example usage:
const year = 2024; // You can change this value
if (isLeapYear(year)) {
console.log(`${year} is a Leap Year.`);
} else {
console.log(`${year} is not a Leap Year.`);
Prime number:
const checkPrime = (num) => {
if (num <= 1) {
return false; // Numbers less than or equal to 1 are not prime
if (num === 2) {
return true; // 2 is the only even prime number
// Check divisibility from 2 to the square root of the number
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false; // Number is divisible by i, so it's not prime
return true; // Number is prime if no divisors found
};
// Example usage
const num = 29; // You can change this to any number
if (checkPrime(num)) {
console.log(`${num} is a Prime Number.`);
} else {
console.log(`${num} is not a Prime Number.`);
Prime number list:
const lower = 1;
const upper = 10;
console.log(`Prime numbers between ${lower} and ${upper} are:`);
for (let num = lower; num <= upper; num++) {
if (num > 1) { // All prime numbers are greater than 1
let isPrime = true;
for (let i = 2; i < num; i++) { // Check divisibility from 2 to num-1
if (num % i === 0) {
isPrime = false;
break;
if (isPrime) {
console.log(num); // If no divisors were found, num is prime
Find the Factorial of a Number
const factorial = (num) => {
let result = 1;
for (let i = 1; i <= num; i++) {
result *= i; // Multiply result by i at each iteration
return result;
};
const num = 5; // You can change this to any number
console.log(`${num}! = ${factorial(num)}`);