🚀 Introduction
When I started learning JavaScript, I did small number-based problems to improve my logic.
In this blog, I will share some basic programs:
Numbers divisible by 3 and 5
Numbers divisible by 3 or 5
Finding factors of a number
Counting factors
Checking prime numbers
These are very useful for beginners to understand loops and conditions.
🔹 1. Numbers divisible by 3 AND 5
let count = 1;
while(count < 100){
if((count % 3 == 0) && (count % 5 == 0)){
console.log(count)
}
count++
}
🧠Explanation:
We check numbers from 1 to 99
Condition is must be divisible by 3 AND 5
Example output:
👉 15, 30, 45, 60, 75, 90
In this case first we are going to create count variable, then we have to entire while loop,
First we are going to know what is while loop ?
while loop is used to repeat the condition again and again, it will stop only when the condition is false.if the condition become false then only the next will run in script.js file.
then we will create if condition to find the count of 99 which is divisible by 3 and 5,if condition inside have console.log(count),
while loop what have is if condition and count++.
why we don't put count++ inside the if condition because if we put inside if condition then count value increase only when the if condition is true, if the if condition is not true while inside the count++, for only in this case while loop is running without end, because if condition is false that only have count++, that reason why we put count++ in while loop
this
🔹 2. Numbers divisible by 3 OR 5
let count = 1;
while(count < 100){
if((count % 3 == 0) || (count % 5 == 0)){
console.log(count)
}
count++
}
this also same like above but what diff is we use ||(or) operator that will make the condition true, if any one is true then the condition become true.
OUTPUT;
🔹 3. Finding Factors of a Number
let givennumber = 10;
let i = 1;
while(i <= givennumber){
if(givennumber % i == 0){
console.log(i)
}
i++
}
🧠Explanation:
We check all numbers from 1 to given number
If remainder is 0 → it is a factor
<< In this case why we use i to increase the numbers 1 to 10 and also
run the loop
👉 Output for 10:
1, 2, 5, 10
4.🔹 4. Count Total Factors
let givennumber = 20;
let i = 1;
let count = 0;
while(i <= givennumber){
if(givennumber % i == 0){
console.log(i)
count++
}
i++
}
console.log(count)
🧠Explanation:
Same as previous
But we also count factors
we create one more variable which is count , here count is used to count the factors, factors means which numbers are divisible in 20 with in 20, in this case we put count++ inside the if condition because assume how many times if condition will run that is the count of factors
OUTPUT;

In this case 1,2,4,5,10,20 is the divisible factors and last one is the factors count that is 6.
5.🔹 5. Check Prime Number
let givennumber = 9;
let i = 1;
let count = 0;
while (i <= givennumber){
if(givennumber % i == 0){
count++
}
i++
}
if(count == 2){
console.log("prime number")
}else{
console.log("not a prime number")
}
🧠Explanation:
A prime number has only 2 factors:
1 and itself
If count == 2 → Prime
this is simple if we know above task
👉 Output for 9:
not a prime number
OUPUT;




Top comments (0)