Important JavaScript Programs which have Appeared in the Last Exams.

Note: Programs below should be written into the <script> tag. 

1. WAP to print fibonacci series.

var a = 0,  b = 1, c, i;  

var num = parseInt (prompt (" Enter the limit for Fibonacci Series "));  

document.write( "Fibonacci Series: ");  

for (i = 1; i <= num; i++)  

{  document.write (" <br> " +  a);  

    c = a + b;      

    a = b;  

    b = c; 

2. WAP to find and print the factorial of a number.

var i, num, f;  

f = 1;  

num = parseInt(prompt("Enter the number to find factorial of: "));  

for(i = 1; i <= num; i++)    

    f = f * i;  

document.write("The factorial of the number " + num + " is: " + f);

3. WAP to check whether the number is prime or not.

int n, i, m = 0, flag = 0;    

n = parseInt(prompt("Enter the number to check prime 

for(i = 2; i <= n/2; i++)    

{    

    if(n % i == 0)    

    {    

        document.write("Number is not prime");    

        flag = 1;    

        break;    

    }        

if(flag == 0)    

    document.write("Number is prime");  

}

4. WAP to find the palindrome of the number.

int n, r, sum = 0, temp;    

n = parseInt(prompt("Enter the number: "));    

temp = n;    

while(n > 0)    

{

    r = n % 10;    

    sum = (sum*10) + r;    

    n = n / 10;    

}    

if(temp == sum)    

    document.write("The number is a palindrome.");    

else    

    document.write("The number is not a palindrome.");

5. WAP to check whether the number is Armstrong or not.

int n, r, sum = 0, temp;    

n = parseInt(prompt("Enter the number: 

temp = n;    

while(n > 0)    

{

    r = n % 10;    

    sum = sum + (r ** 3);    

    n = n / 10;    

}    

if(temp == sum)    

    document.write("The number is an Armstrong.");    

else    

    document.write("The number is not an Armstrong.");

6. WAP to find the largest number among three numbers.

const num1 = parseInt(prompt("Enter first number: "));
const num2 = parseInt(prompt("Enter second number: "));
const num3 = parseInt(prompt("Enter third number: "));
let largest;

if(num1 >= num2 && num1 >= num3) {
    largest = num1;
}
else if (num2 >= num1 && num2 >= num3) {
    largest = num2;
}
else {
    largest = num3;
}

document.write("The largest number is " + largest);

7. WAP to take numbers as input and ask the user to select the operator and print the result as per the user's selection.

const operator = prompt("Select the operation ( either +, -,* or / ): ");

const number1 = parseInt(prompt("Enter first number: "));
const number2 = parseInt(prompt("Enter second number: "));

let result;

if (operator == '+') {
    document.write(number1 + operator + number2 " = " result);
}
else if (operator == '-') {
    document.write(number1 + operator + number2 " = " result);
}
else if (operator == '*') {
    document.write(number1 + operator + number2 " = " result);
}
else if (operator == '/') {
    document.write(number1 + operator + number2 " = " result);
}
else{
    document.write("The chosen operator is invalid!");
}

8. WAP to take a number from the user and find its output.

const number = parseInt(prompt('Enter an integer: '));

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

    const result = i * number;

    document.write(number " * " i " = " result + "<br>");
}

9. WAP to find the factor of the number.

const num = prompt('Enter a positive number: ');

document.write("The factors of " + num + " is: <br>");

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

    if(num % i == 0) {
        document.write(i);
    }
}

10. WAP to solve the given quadratic equation.

This is a very complicated topic so far. Look at the code and wonder for some time.

let root1, root2;

let a = prompt("Enter the first number: ");
let b = prompt("Enter the second number: ");
let c = prompt("Enter the third number: ");

let discriminant = b ** 2 - 4 * a * c; //calculating discriminant to find the nature of the equation

// condition for real and different roots
if (discriminant > 0) {
    root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
    root2 = (-b - Math.sqrt(discriminant)) / (2 * a);

    // result
    document.write("The roots of quadratic equation are " + root1 + " and " + root2");
}

// condition for real and equal roots
else if (discriminant == 0) {
    root1 = root2 = -b / (2 * a);

    document.write("The roots of quadratic equation are " + root1 + " and " + root2");
}

// if roots are not real
else {
    let realPart = (-b / (2 * a)).toFixed(2);
    let imagPart = (Math.sqrt(-discriminant) / (2 * a)).toFixed(2);

  document.write("The roots of quadratic equation are ", realPart, "+", imagPart, "i", " and ", realPart, "-", imagPart, "i");
}






Next ---> Computer Science - JavaScript (Part 2 - Advance)




Notes By Bishal Subedi (TheB).
Published by NotesPT, visit www.notespt.ml for more.



Thanks for visiting!