lOMoARcPSD|530 626 76
lOMoARcPSD|530 626 76
Tribhuvan University
Faculty of Humanities and Social Sciences
A LAB REPOT
On
"Scripting Language"
Submitted To:
Department of Bachelor in Computer Application
Mahendra Multiple Campus
Nepalgunj, Banke
In partial fulfillment of the requirements for the Bachelors in Computer Application
Submitted By:
Dinesh Sharma
TU Registration No.: 6-2-55-60-2022
Symbol No: 5502115
Submitted To:
Er. Ramesh Tharu
Table Of Contents
Cover Page……………………………………………………………………………………… 1
Table of Contents………………………………………………………………………………… 2
Question 1……………………………………………………………………………… 3
1
lOMoARcPSD|530 626 76
Question 2……………………………………………………………………………… 3
Question 3……………………………………………………………………………… 3
Question 4……………………………………………………………………………… 4
Question 5……………………………………………………………………………… 5
Question 6……………………………………………………………………………… 5
Question 7……………………………………………………………………………… 6
Question 8……………………………………………………………………………… 6
Question 9……………………………………………………………………………… 7
Question 10……………………………………………………………………………… 7
Question 11……………………………………………………………………………… 8
Question 12……………………………………………………………………………… 8
Question 13……………………………………………………………………………… 8
Question 14……………………………………………………………………………… 9
Question 15……………………………………………………………………………… 9
Question 16……………………………………………………………………………… 10
Question 17……………………………………………………………………………… 10
Question 18……………………………………………………………………………… 10
Question 19……………………………………………………………………………… 11
Question 20……………………………………………………………………………… 12
Question 21……………………………………………………………………………… 12
Question 22……………………………………………………………………………… 13
Question 23……………………………………………………………………………… 15
Question 24……………………………………………………………………………… 15
Question 25……………………………………………………………………………… 16
Question 26……………………………………………………………………………… 16
Question 27……………………………………………………………………………… 16
Question 28……………………………………………………………………………… 17
Question 29……………………………………………………………………………… 17
Question 30……………………………………………………………………………… 18
1. WAP to find sum of two numbers let num1 =
parseFloat(prompt("Enter the first number:")); let num2 =
parseFloat(prompt("Enter the second number:"));
if (isNaN(num1) || isNaN(num2)) {
document.write("Invalid input. Please enter numbers.<br>");
lOMoARcPSD|530 626 76
} else {
let sum = num1 + num2;
document.write("Sum of two numbers: " + num1 + " + " + num2 + " = " + sum +
"<br><br>");
}
Output:
2. WAP to find product of two numbers
num1 = parseFloat(prompt("Enter the first number:")); num2
= parseFloat(prompt("Enter the second number:"));
if (isNaN(num1) || isNaN(num2)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
let product = num1 * num2;
document.write("Product of two numbers: " + num1 + " * " + num2 + " = " + product +
"<br><br>");
}
Output:
3. WAP to add, subtract, multiply, and divide two numbers
num1 = parseFloat(prompt("Enter the first number:")); num2
= parseFloat(prompt("Enter the second number:"));
if (isNaN(num1) || isNaN(num2)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
3
lOMoARcPSD|530 626 76
let sum = num1 + num2; let
difference = num1 - num2; let
product = num1 * num2;
let quotient = num2 === 0 ? "Cannot divide by zero" : num1 / num2;
document.write("Operations on two numbers:<br>");
document.write(num1 + " + " + num2 + " = " + sum + "<br>");
document.write(num1 + " - " + num2 + " = " + difference + "<br>");
document.write(num1 + " * " + num2 + " = " + product + "<br>");
document.write(num1 + " / " + num2 + " = " + quotient + "<br><br>");
}
Output :
4. WAP to find simple interest
let principal = parseFloat(prompt("Enter the principal amount:"));
let time = parseFloat(prompt("Enter the time (in years):")); let rate
= parseFloat(prompt("Enter the rate of interest:"));
if (isNaN(principal) || isNaN(time) || isNaN(rate)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
let si = (principal * time * rate) / 100;
document.write("Simple Interest: (" + principal + " * " + time + " * " + rate + ") / 100 =
" + si + "<br><br>");
}
Output:
lOMoARcPSD|530 626 76
5. WAP to find area of rectangle
let length = parseFloat(prompt("Enter the length of the rectangle:")); let
breadth = parseFloat(prompt("Enter the breadth of the rectangle:"));
if (isNaN(length) || isNaN(breadth)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
let area = length * breadth;
document.write("Area of Rectangle: " + length + " * " + breadth + " = " + area +
"<br><br>");
}
Output:
6. WAP to find area of circle
let radius = parseFloat(prompt("Enter the radius of the circle:"));
if (isNaN(radius)) {
document.write("Invalid input. Please enter a number.<br>");
} else {
const pi = Math.PI; let area
= pi * radius * radius;
document.write("Area of Circle:
pi * " + radius + " * " + radius +
" = " + area + "<br><br>");
}
Output:
5
lOMoARcPSD|530 626 76
7. WAP to find largest among two numbers num1 =
parseFloat(prompt("Enter the first number:")); num2 =
parseFloat(prompt("Enter the second number:"));
if (isNaN(num1) || isNaN(num2)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
let largest = num1 > num2 ? num1 : num2;
document.write("Largest among two: " + num1 + " and " + num2 + " - Largest is " +
largest + "<br><br>");
}
Output:
8. WAP to find smallest among two numbers num1 =
parseFloat(prompt("Enter the first number:")); num2 =
parseFloat(prompt("Enter the second number:"));
if (isNaN(num1) || isNaN(num2)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
let smallest = num1 < num2 ? num1 : num2;
document.write("Smallest among two: " + num1 + " and " + num2 + " - Smallest is " +
smallest + "<br><br>");
}
Output
lOMoARcPSD|530 626 76
9. WAP to find largest among three numbers num1 =
parseFloat(prompt("Enter the first number:")); num2 =
parseFloat(prompt("Enter the second number:")); let
num3 = parseFloat(prompt("Enter the third number:"));
if (isNaN(num1) || isNaN(num2) || isNaN(num3)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
let largest = Math.max(num1, num2, num3);
document.write("Largest among three: " + num1 + ", " + num2 + ", and " + num3 + " -
Largest is " + largest + "<br><br>");
}
Output:
10. WAP to find smallest among three numbers num1 =
parseFloat(prompt("Enter the first number:")); num2 =
parseFloat(prompt("Enter the second number:"));
num3 = parseFloat(prompt("Enter the third number:"));
if (isNaN(num1) || isNaN(num2) || isNaN(num3)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
let smallest = Math.min(num1, num2, num3);
document.write("Smallest among three: " + num1 + ", " + num2 + ", and " + num3 + " -
Smallest is " + smallest + "<br><br>");
}
Output:
7
lOMoARcPSD|530 626 76
11. WAP to check whether a number is odd or even let
num = parseInt(prompt("Enter a number:"));
if (isNaN(num)) {
document.write("Invalid input. Please enter a number.<br>");
} else {
let result = num % 2 === 0 ? "Even" : "Odd";
document.write(num + " is " + result + "<br><br>");
}
Output:
12. WAP to check whether a number is divisible by 7 or
not num = parseInt(prompt("Enter a number:"));
if (isNaN(num)) {
document.write("Invalid input. Please enter a number.<br>");
} else {
let result = num % 7 === 0 ? "Divisible by 7" : "Not divisible by 7";
document.write(num + " is " + result + "<br><br>");
}
Output:
13. WAP to check whether a number is exactly divisible by
5 and 10 num = parseInt(prompt("Enter a number:"));
if (isNaN(num)) {
document.write("Invalid input. Please enter a number.<br>");
lOMoARcPSD|530 626 76
} else { let result = (num % 5 === 0 && num % 10 === 0) ? "Divisible by both 5
and 10" :
"Not divisible by both 5 and 10";
document.write(num + " is " + result + "<br><br>");
}
Output:
14. WAP to check whether a number is divisible by 7 but
not by 13 num = parseInt(prompt("Enter a number:"));
if (isNaN(num)) {
document.write("Invalid input. Please enter a number.<br>");
} else { let result = (num % 7 === 0 && num % 13 !== 0) ? "Divisible by 7 but not
by 13" :
"Does not satisfy the condition";
document.write(num + " is " + result + "<br><br>");
}
Output:
15. WAP to input CP and SP and check profit or loss. Also
find profit or loss amount let cp =
parseFloat(prompt("Enter Cost Price:")); let sp =
parseFloat(prompt("Enter Selling Price:"));
if (isNaN(cp) || isNaN(sp)) {
document.write("Invalid input. Please enter numbers.<br>");
} else {
9
lOMoARcPSD|530 626 76
let result = sp > cp ? "Profit" : sp < cp ? "Loss" : "No Profit No Loss";
let amount = Math.abs(sp - cp);
document.write(result + " of amount: " + amount + "<br><br>");
}
Output:
16. WAP to print numbers from 1 to 10 for (let i = 1; i <=
10; i++) { document.write(i + " ");
}
document.write("<br><br>"); Output:
17. WAP to find sum of numbers from 5 to 100 let sum =
0;
for (let i = 5; i <= 100; i++) {
sum += i;
}
document.write("Sum from 5 to 100: " + sum + "<br><br>"); Output:
18. WAP to print following series
a. 5, 10, 15, 20, …… 50 for
(let i = 5; i <= 50; i += 5) {
document.write(i + " ");
}
document.write("<br>"); Output:
lOMoARcPSD|530 626 76
b. 1, 4, 9, 16, ….. upto 20
terms for (let i = 1; i <= 20; i++) {
document.write(i * i + " ");
}
document.write("<br>"); Output:
c. 100, 98, 96, 94, ………….
Upto 10 terms for (let i = 100,
count = 0; count < 10; i -= 2,
count++) { document.write(i + "
");
}
document.write("<br><br>"); Output:
19. WAP to print first 15 even numbers for (let i = 2,
count = 0; count < 15; i += 2, count++) {
document.write(i + " ");
}
document.write("<br><br>"); Output:
20. WAP to find sum of odd numbers from 1 to 100
sum = 0;
for (let i = 1; i <= 100; i += 2) {
sum += i;
}
document.write("Sum of odd numbers from 1 to 100: " + sum + "<br><br>"); Output:
11
lOMoARcPSD|530 626 76
21. WAP to find factorial of a given number num =
parseInt(prompt("Enter a number:")); if
(isNaN(num) || num < 0) {
document.write("Invalid input. Please enter a non-negative number.<br>");
} else { let factorial = 1; for
(let i = 1; i <= num; i++) {
factorial *= i;
}
document.write("Factorial of " + num + " is " + factorial + "<br><br>"); }
Output:
22. WAP to print Fibonacci series up to 15 terms let
n1 = 1, n2 = 1, nextTerm; document.write(n1 + " "
+ n2 + " "); for (let i = 3; i <= 15; i++) {
nextTerm = n1 + n2; document.write(nextTerm
+ " "); n1 = n2; n2 = nextTerm;
}
document.write("<br><br>"); Output:
23. WAP to print following patterns
a.
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= i; j++) {
document.write("*");
}
document.write("<br>");
}
lOMoARcPSD|530 626 76
document.write("<br>"); Output:
b.
for (let i = 5; i >= 1; i--) {
for (let j = 1; j <= i; j++) {
document.write("*");
}
document.write("<br>");
}
document.write("<br>"); Output:
c.
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= i; j++) {
document.write(j);
}
document.write("<br>");
}
document.write("<br>"); Output:
13
lOMoARcPSD|530 626 76
d.
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= i; j++) {
document.write(i);
}
document.write("<br>");
}
document.write("<br>"); Output:
e.
for (let i = 1; i <= 5; i++) {
let num = ""; for (let j = 1;
j <= i; j++) { num +=
"1";
}
document.write(num + "<br>");
}
document.write("<br>"); Output:
lOMoARcPSD|530 626 76
24. WAP to check whether a number is
prime or not let num = parseInt(prompt("Enter
a number:")); let isPrime = num > 1;
for (let i = 2; i < num; i++) {
if (num % i === 0) {
isPrime = false; break;
}
}
document.write(num + " is " + (isPrime ? "Prime" : "Not Prime") + "<br><br>"); Output:
25. WAP to print prime numbers from 1 to
100 for (let num = 2; num <= 100; num++) {
let isPrime = true; for (let i = 2; i < num;
i++) {
if (num % i === 0) {
isPrime = false; break;
} } if
(isPrime) {
document.write(num + " ");
}
15
lOMoARcPSD|530 626 76
}
document.write("<br><br>"); Output:
26. WAP to show the use of ternary operator
num = parseInt(prompt("Enter a number:"));
document.write(num + " is " + (num % 2 === 0 ? "Even" : "Odd") + "<br><br>");
Output:
27. Write a program to show the use of switch
case statement let day = parseInt(prompt("Enter
a number (1-7):"));
switch (day) {
case 1: document.write("Sunday"); break;
case 2: document.write("Monday"); break;
case 3: document.write("Tuesday"); break;
case 4: document.write("Wednesday"); break;
case 5: document.write("Thursday"); break;
case 6: document.write("Friday"); break; case
7: document.write("Saturday"); break;
default: document.write("Invalid input");
}
document.write("<br><br>"); Output:
lOMoARcPSD|530 626 76
28. WAP to read a number & find out if it is an
Armstrong number or not num = parseInt(prompt("Enter a
number:")); let temp = num, sum = 0, digits =
num.toString().length;
while (temp > 0) { let digit =
temp % 10; sum +=
Math.pow(digit, digits); temp =
Math.floor(temp / 10);
}
document.write(num + " is " + (sum === num ? "an Armstrong number" : "not an
Armstrong number") + "<br><br>"); Output:
29. WAP to generate Armstrong numbers from 1 to
100 for (let num = 1; num <= 1000; num++) {
let temp = num, sum = 0, digits = num.toString().length;
while (temp > 0) { let digit =
temp % 10; sum +=
Math.pow(digit, digits); temp =
Math.floor(temp / 10);
}
if (sum === num) {
document.write(num + " ");
}
}
document.write("<br><br>"); Output:
17
lOMoARcPSD|530 626 76
30. WAP to display a multiplication table dynamically
const start = parseInt(prompt("Enter starting value for N:"));
const end = parseInt(prompt("Enter ending value for N:"));
if (isNaN(start) || isNaN(end)) {
document.write("Please enter valid numbers.");
} else {
// Determine actual range (handle reverse input)
const actualStart = Math.min(start, end); const
actualEnd = Math.max(start, end);
let table = `
<table border="1" cellpadding="5" style="border-collapse: collapse; margin: 20px;">
<tr>
<th>N</th>
<th>10 * N</th>
<th>100 * N</th>
<th>1000 * N</th>
</tr>`;
for (let n = actualStart; n <= actualEnd; n++) {
table += `
<tr>
<td>${n}</td>
<td>${10 * n}</td>
<td>${100 * n}</td>
<td>${1000 * n}</td>
</tr>`;
lOMoARcPSD|530 626 76
table += "</table>";
document.write(table); } Output :
31. WAP to calculate compound interest
let principal = parseFloat(prompt("Enter the principal amount:"));
let rate = parseFloat(prompt("Enter the rate of interest:")); let time
= parseFloat(prompt("Enter the number of years:"));
let compoundInterest = principal * (Math.pow((1 + rate / 100), time) - 1);
document.write("Compound Interest: " + compoundInterest.toFixed(2) + "<br><br>");
Output:
19