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

JavaScript Basics: Math and Logic

The document contains JavaScript code snippets demonstrating various programming concepts such as arithmetic operations with three variables, finding the largest of three numbers, checking if a number is even or odd, determining pass or fail based on percentage, string concatenation, checking for Armstrong numbers, calculating factorials, checking for palindrome numbers, and identifying leap years. Each section includes code that performs the respective operation and outputs the result to the document. The examples cover fundamental programming logic and operations in JavaScript.

Uploaded by

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

JavaScript Basics: Math and Logic

The document contains JavaScript code snippets demonstrating various programming concepts such as arithmetic operations with three variables, finding the largest of three numbers, checking if a number is even or odd, determining pass or fail based on percentage, string concatenation, checking for Armstrong numbers, calculating factorials, checking for palindrome numbers, and identifying leap years. Each section includes code that performs the respective operation and outputs the result to the document. The examples cover fundamental programming logic and operations in JavaScript.

Uploaded by

Kishore Bappuraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Arithmetic with 3 variables


let a = 10;
let b = 5;
let c = 2;

[Link]("Addition = " + (a + b + c) + "<br>");


[Link]("Subtraction = " + (a - b - c) + "<br>");
[Link]("Multiplication = " + (a * b * c) + "<br>");
[Link]("Average = " + ((a + b + c) / 3) + "<br>");
[Link]("Average = " + ((a + b + c) % 3) + "<br>");

2. Largest of 3 numbers

let n1 = 10
let n2 = 25
let n3 = 12

let largest;

if (n1 >= n2 && n1 >= n3) {


largest = n1;
} else if (n2 >= n1 && n2 >= n3) {
largest = n2;
} else {
largest = n3;
}

[Link]("Largest number = " + largest + "<br>");

3. Even or odd
let num = 56

if (num % 2 === 0) {
[Link](num + " is even<br>");
} else {
[Link](num + " is odd<br>");
}

4. Pass or fail using percentage

let per = 94.67

if (per >= 40) {


[Link]("Result: Pass<br>");
} else {
[Link]("Result: Fail<br>");
}

5. Example of string operator


let firstName = "Hemant";
let lastName = "Singh";
let fullName = firstName + " " + lastName;

[Link]("Full name = " + fullName + "<br>");

6. Armstrong number (3-digit)


let number = 343
let temp = number;
let sum = 0;

while (temp > 0) {


let digit = temp % 10;
sum = sum + digit * digit * digit;
temp = parseInt(temp / 10);
}

if (sum === number) {


[Link](number + " is an Armstrong number<br>");
} else {
[Link](number + " is not an Armstrong number<br>");
}

7. Factorial of a number
js
let n = 5
let fact = 1;

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


fact = fact * i;
}

[Link]("Factorial of " + n + " = " + fact + "<br>");

8. Palindrome number or not


let numPal = 121
let original = numPal;
let reverse = 0;

while (numPal > 0) {


let digit = numPal % 10;
reverse = reverse * 10 + digit;
numPal = parseInt(numPal / 10);
}

if (reverse === original) {


[Link](original + " is a palindrome number<br>");
} else {
[Link](original + " is not a palindrome number<br>");
}

9. Leap year or not


let year = 2014

if (year % 4 === 0) {
[Link](year + " is a leap year<br>");
} else {
[Link](year + " is not a leap year<br>");
}
www

You might also like