0% found this document useful (0 votes)
42 views4 pages

Nested Loops New Questions

New questions for practice on nested loops

Uploaded by

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

Nested Loops New Questions

New questions for practice on nested loops

Uploaded by

Pace Infotech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Calculating Sum of Multiples

Question: Write a JavaScript function that calculates the sum of all multiples of x up to a limit n. For
example, for x = 3 and n = 20, the function should sum up all multiples of 3 up to 20 (3, 6, 9, 12, 15,
18) and return the total sum.

Explanation: Use nested loops where the outer loop iterates through multiples of x and checks if
they are less than n, accumulating the sum.

2. Finding Common Factors

Question: Write a JavaScript function that finds and prints all common factors of two given numbers
a and b. For example, for a = 12 and b = 18, the common factors are 1, 2, 3, and 6.

Explanation: Use nested loops to iterate through possible factors and check if both numbers are
divisible by each factor.

3. Summing Digits of a Range

Question: Write a JavaScript function that calculates the sum of all digits of numbers from 1 to n. For
example, for n = 12, the function should compute the sum of digits for numbers 1 through 12 (1 + 2 +
3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 0 + 1 + 2) and return the total sum.

Explanation: Use nested loops where the outer loop iterates through numbers from 1 to n, and the
inner loop extracts and sums the digits of each number.

4. Calculating Powers

Question: Write a JavaScript function that calculates and prints the power of numbers from 1 to n for
an exponent e. For example, for n = 3 and e = 2, the function should calculate and print:

1^2 = 1

2^2 = 4

3^2 = 9

Explanation: Use nested loops where the outer loop iterates through numbers from 1 to n, and the
inner loop performs the exponentiation.

5. Counting Divisors

Question: Write a JavaScript function that counts the number of divisors for each number from 1 to
n. For example, for n = 5, the output should be:

1 has 1 divisor

2 has 2 divisors

3 has 2 divisors

4 has 3 divisors

5 has 2 divisors
Explanation: Use nested loops where the outer loop iterates through numbers from 1 to n, and the
inner loop counts how many numbers divide each number without leaving a remainder.

6. Generating Pascal’s Triangle

Question: Write a JavaScript function that generates Pascal’s Triangle up to n rows. For example, for
n = 4, the output should be:

11

121

1331

Explanation: Use nested loops where the outer loop iterates through rows, and the inner loop
calculates the values in each row based on the values from the previous row.

7. Generating a Square Pattern

Question: Write a JavaScript function that generates a square pattern of n rows and n columns using
* characters. For example, for n = 4, the output should be:

markdown

****

****

****

****

Explanation: Use nested loops where the outer loop iterates over the rows and the inner loop prints
the * characters for each column in a row.

8. Fibonacci Triangle

Question: Write a JavaScript function that prints a right-angled triangle where each row contains the
Fibonacci sequence up to that row’s length. For example, for n = 4, the output should be:

11

112

1123

Explanation: Use nested loops to compute and print the Fibonacci sequence. The outer loop handles
the rows, and the inner loop prints the Fibonacci numbers for each row.
9. Factorial Calculation

Question: Write a JavaScript function to calculate the factorial of numbers from 1 to n using nested
loops. For example, for n = 3, the output should be:

1! = 1

2! = 2

3! = 6

Explanation: Use an outer loop to iterate through numbers from 1 to n, and an inner loop to
calculate the factorial of each number.

10. Diamond Pattern

Question: Write a JavaScript function that prints a diamond pattern of n rows (where n is an odd
number). For example, for n = 5, the output should be:

markdown

***

*****

***

Explanation: Use nested loops to print spaces and stars. The outer loop handles the rows, and the
inner loop prints the correct number of spaces and stars for each row.

11. Multiplication Table

Question: Write a JavaScript function to generate and print a multiplication table for numbers from 1
to n. For example, for n = 4, the output should be:

1234

2468

3 6 9 12

4 8 12 16

Explanation: Use nested loops where the outer loop iterates over rows and the inner loop computes
the product of the row and column indices.
12. Number Pyramid

Question: Write a JavaScript function to print a pyramid pattern of numbers. For example, for n = 3,
the output should be:

121

12321

Explanation: Use nested loops to print spaces and numbers. The outer loop handles the rows, and
the inner loops print spaces and numbers to create the pyramid shape.

You might also like