DEV Community

Madhavan G
Madhavan G

Posted on

Some problems in Looping.

1.Multiples of 3 and 5
Code:
let i=1;
while(i<50){
if(i%3==0 && i%5==0){
console.log(i);
}
i++;
}
What the code is doing:

  1. - It Start from i = 1
  2. - It Will Loop until i < 50
  3. - Check if i is divisible by both 3 and 5
  4. - If yes → Then it will be printed
  5. - Increase i every time

Screenshot of the Program:

2.Prime Number
code:
let count=0;
let num=5;
let i=1;
while(i<=num){
if(num%i==0){
count++;
}
i++
}
if(count==2){
console.log("It is a PrimeNumber")
}else{
console.log("It not a PrimeNumber")
}

What the code is doing:

  1. - Start with i = 1
  2. - Loop until i <= num
  3. - Check if num is divisible by i
  4. - If yes → increase count
  5. - Increase i every time
  6. - After loop ends: - If count == 2 → print Prime Number - Else → print Not a Prime Number

Screenshot of the Program:

3.count of digits
Code:
let num=12345;
let count=0;
while(num>0){
num= Math.floor(num/10);
count++;
}
console.log(count)

What the code is doing:

  1. - Start with num = 12345
  2. - Start with count = 0
  3. - Loop until num > 0
  4. - Inside the loop: - Remove the last digit of num → num = Math.floor(num / 10) - Increase count by 1
  5. - Repeat the loop
  6. - After the loop ends → print count

Screenshot of the Program:

Top comments (0)