0% found this document useful (0 votes)
21 views

CSS - Practical 4

Uploaded by

anasqureshi6556
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

CSS - Practical 4

Uploaded by

anasqureshi6556
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CSS- PRACTICAL 4

Name:- Anas Qureshi


Roll No:- 220440
---------------------------------------------------------------------------------------------------------------------------
Q1. Program to create, read and display the array using literal and constructor.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
let number=prompt("Enter The Number")
function isPalindrome(number) {
const str = number.toString();
const revstr = str.split("").reverse().join("");
return str == revstr;
}
if (isPalindrome(number)) {
document.write(number+" is Palindrome");
} else {
document.write(number+" is not Palindrome");
}
</script>
</head>
<body>
</body>
</html>
Output: -
Q2. Write a program to print the reverse of n digit number using function.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
let number = parseInt(prompt("Enter a number: "));
let rem = 0;
let rev = 0;
while (number != 0) {
rem = number % 10;
rev = (rev * 10) + rem;
number = Math.floor(number / 10);
}
document.write("Reverse: " + rev);
</script>
</head>
<body>
</body>
</html>
Output: -

Q3. Write a program to print the sum of n digit number using method.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
let num = parseInt(prompt("Enter the Number : "));
let rem = 0;
let sum = 0;
while (num != 0) {
rem = num % 10;
sum += rem;
num = Math.floor(num / 10);
}
document.write("Sum of digits of entered number is : " + sum);
</script>
</head>
<body>
</body>
</html>
Output: -

Q4. Write a program to check whether number is an Armstrong or not using method.
Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
function isArmstrong(number) {
let sum = 0;
let temp = number;
const numDigits = number.toString().length;
while (temp > 0) {
let digit = temp % 10;
sum += digit ** numDigits;
temp = Math.floor(temp / 10);
}
return sum === number;
}
const number = parseInt(prompt("Enter a number: "), 10);
if (isArmstrong(number)) {
document.write(number + " is an Armstrong number.");
} else {
document.write(number + " is not an Armstrong number.");
}
</script>
</head>
<body>
</body>
</html>
Output: -

Q5. Write a program to print the factorial series using constructor.


Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
class Factorial {
constructor(n) {
this.n = n;
}
static calculate(num) {
if (num == 0 || num == 1) {
return 1;
} else {
return num * Factorial.calculate(num - 1);
}
}
getFactorial() {
return Factorial.calculate(this.n);
}
}
var f = new Factorial(
parseInt(prompt("Enter the number whose Factorial you want : "))
);
var fact = f.getFactorial();
document.write("Factorial is: " + fact);
</script>
</head>
<body>
</body>
</html>
Output: -

Q6. Write a program to print the Fibonacci series using constructor.


Code: -
<html>
<head>
<title>Anas Qureshi-220440</title>
<script>
class Fibo {
constructor(n) {
this.n = n;
}
calculateFibonacci() {
let n1 = 0, n2 = 1, n3;
document.write("Fibonacci Series:<br>");
if (this.n >= 1) document.write(n1 + "<br>");
if (this.n >= 2) document.write(n2 + "<br>");

for (let i = 2; i < this.n; i++) {


n3 = n1 + n2;
document.write(n3 + "<br>");
n1 = n2;
n2 = n3;
} }}
const num = parseInt(prompt("Enter the number of terms: "), 10);
const f = new Fibo(num);
f.calculateFibonacci();
</script>
</head>
<body>
</body>
</html>
Output: -

You might also like