Loops Practice Problems - JS

  1. Write a JavaScript program that uses a for loop to print out the numbers from 1 to 10

  2. Write a JavaScript program that uses a while loop to print out the numbers from 1 to 10.

  3. Write a JavaScript program that uses a for loop to calculate the sum of all numbers from 1 to 10.

  4. Write a JavaScript program that uses a for loop to print out the multiplication table for the number 5.

  5. Write a JavaScript program that uses a while loop to print out the Fibonacci sequence up to the number 100.

2 Likes

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

    }
  1. console.log( ‘for loop’);
    for(let i=1 ; i <=10;i++)
    {
    console.log(i);
    }

let i=1;
while(i<=10){
i++;
console.log(i)
}

for (var input = 1; input <= 10; input++) {
console.log(input);
}

et i = 1;
while (i <= 10) {
console.log(i);
i++;
}

let sum = 0;
for (let i = 1; i <= 10; i++) {
sum += i;
}
console.log(sum);
4.

for (let i = 1; i <= 10; i++) {
  console.log(`5 x ${i} = ${5 * i}`);
}

5.
let a = 0;
let b = 1;
let c = 0;

while (c <= 100) {
  console.log(c);
  a = b;
  b = c;
  c = a + b;
}
  1. for(let i=1;i<=10;i++){
    console.log(i)
    }

2.let i=1;
while(i<=10){
console.log(i);
i++;
}

3.let sum=0;
for(let i=1;i<=10;i++){
sum+=i;
}

4.let x=5;
for(let i=1;i<=10;i++){
console.log(x*i)
}

1.for (let i = 1; i <= 10; i++) {
console.log(i);
}

  1. let i = 1;
    while (i <= 10) {
    console.log(i);
    i++;
    }

  2. let sum = 0;

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

console.log(" " + sum);

  1. for (let i = 1; i <= 10; i++) {
    let result = i * 5;
    console.log(5 x ${i} = ${result});
    }

  2. let num1 = 0, num2 = 1, nextTerm;

console.log(“Fibonacci Sequence:”);

for (let i = 1; num1 <= 100; i++) {
console.log(num1);
nextTerm = num1 + num2;
num1 = num2;
num2 = nextTerm;
}

// 1.Write a JavaScript program that uses a for loop to print out the numbers from 1 to 10
for(let i=0;i<=10;i++){
console.log(i);
}
//2.Write a JavaScript program that uses a while loop to print out the numbers from 1 to 10.
var numbers =0;
while(numbers<10){
numbers+=1;
console.log(numbers);
}
//3.Write a JavaScript program that uses a for loop to calculate the sum of all numbers from 1 to 10

    let sum = 0;
    const n = 10;
    for (let i = 1; i <= n; i++) {
      sum += i;
    }
   console.log("sum:", sum);
  //4.Write a JavaScript program that uses a for loop to print out the multiplication table for the number 5

  const num = 5;
  for (let i = 1; i <= 10; i++) {
    const result = i * num;

    console.log(`${num} * ${i} = ${result}`);
  }
  //5.Write a JavaScript program that uses a while loop to print out the Fibonacci sequence up to the number 100.
  // program to generate fibonacci series up to a certain number

  // take input from the user
  const number = 100;
  let n1 = 0,
    n2 = 1,
    nextTerm;

  console.log("Fibonacci Series:");
  console.log(n1); 
  console.log(n2); 

  nextTerm = n1 + n2;

  while (nextTerm <= number) {
    console.log(nextTerm);

    n1 = n2;
    n2 = nextTerm;
    nextTerm = n1 + n2;
  }