Today I post a some example programs of while loop using Java script.this all programs are the technical Programs of some companies to ask their technical rounds.
1️⃣ MULTIPLES OF 3 AND 5?
To find multiples of 3 and 5 it means what number divisible by 3 and 5 is called multiples.
Find the multiples use the modulo operator (%).This operator used for find a remaider value otherwise division (/) operator used for only quotient.
STEPS:
✓First take the initial value
i = 3.because 3 is starting value from given number.
✓Then give the condition , assume final value is 100, so take the value (i<=100)
✓Next check given number divisible by i,so give(i % 3 == 0 && i % 5 == 0).
✓ Print the value of i.
✓Then increment the value of i.
WORKING:
1.First take given value 3.
2.Then check the condition (i<=100).
3.Next check the if condition (i % 3 == 0 && i % 5 == 0) it's makes sure the which number gets divided and gets remainder 0.
◾ i is 3 .(3<=100 true).then check the if condition.(3%3==0 true) Because remainder is 0(0==0 true) and(&&) (3%5==0 false) remainder is 2 (2==0 false).In (&&) operator is if the both condition evaluates true print true.if one of them gets false then print false.In this program one is true and another is false
◾ This process is repeated untill the both 3 and 5 gets true.
◾ i is 15.(15<=100 true).then check the if condition.(15%3==0 true) Because remainder is 0(0==0 true) and(&&) (15%5==0 false) remainder is 0(0==0 true).both condition is true so print 15.
◾ 30,45,60,75,90 these all numbers are divisible by both 3 and 5 in between 3 and 100.
◾ i is 101.(101<=100 false).loop will breaks.
4.Print i only if the condition is true.otherwise it's skip.
5.Increment the value of i.again check the next value.
OUTPUT:
2️⃣ MULTIPLES OF 3 OR 5?
To find the multiples of 3 or 5.
PROGRAM:
WORKING:
1.First take given value 3.
2.Then check the condition.I need the values till 50.so I take (i<=50).
3.Next check the if condition (i % 3 == 0 || i % 5 == 0) it's makes sure the which number gets divided and gets remainder 0.
◾ i is 3 .(3<=50 true).then check the if condition.(3%3==0 true) Because remainder is 0(0==0 true) OR (||) (3%5==0 false) remainder is 2 (2==0 false).In (&&) operator is if the both condition evaluates true print true, but the OR(||) operator any one condition evaluates true it prints true.dosen't consider one false condition. but if both of them gets false then print false.In this program one is true and another is false.so print in next step.
◾ i is 4.(4<=50 true).then check the if condition.(4%3==0 false)(4%5==0 false).both condition is false. so, the value not satisfy the condition.then skip.
◾ This process is repeated untill the 3 or 5 anyone becomes true inbetween the 3 to 50.
◾ i is 5.(5<=50 true).then check the if condition.(5%3==0 false) Because remainder is 0(2==0 false) and(&&) (5%5==0 true) remainder is 0(0==0 true).both condition is true so print 5.
◾ these all numbers are divisible by both 3 and 5 in between 3 and 100.
◾ i is 51.(51<=50 false).loop will breaks.
4.Print i only if the condition is true.otherwise it's skip.
5.Increment the value of i.
OUTPUT:
3️⃣ DIVISORS OF GIVEN NUMBER:
Divisors of numbers means what are the numbers divides a particular one number to gets remainder 0.
example:12(1,2,3,4,6,12). The number 12 is divided these numbers and gets remainder 0.
STEPS:
✓First take the given number. I take 10.so write givennumber=10.
✓Then take the initial value
i = 1.because we don't know what the numbers are divide 10.
✓Then give the condition , given value is 10, so take the value (i<=10)
✓Next check given number divisible by i,so give(givennumber % i == 0).
✓ Print the value of i.
✓Then increment the value of i.
PROGRAM:
WORKING:
1.First take given value 1.
2.Then check the condition.(i<=10).
◾ i is 1.(i<=10 true).Then go next step.
3.Next check the if condition (givennumber % i == 0).
◾ (givennumber(10) % 1== 0 true).because the 10 is divided by 1 and the remaider is 0.
4.condition is true.so, print the number.
5.i++. increment the value of i.then again go to check next value.
◾ Then i is 2.(2<=10 true).Then go next step.
◾ (givennumber(10) % 2== 0 true).because the 10 is divided by 2 and the remaider is 0.
◾ Then i is 3.(3<=10 true).Then go next step.
◾ (givennumber(10) % 3== 0 false).because the 10 is not divided by 3 and the remaider is 1.
◾ Then i is 4.(4<=10 true).(givennumber(10) % 4== 0 false)
◾ Then i is 5.(5<=10 true).(givennumber(10) % 5== 0 true).Then print 5.
◾ Then i is 6.(6<=10 true).(givennumber(10) % 6== 0 false)
◾ Then i is 7.(7<=10 true).(givennumber(10) % 7== 0 false)
◾ Then i is 8.(8<=10 true).(givennumber(10) % 8== 0 false)
◾ Then i is 9.(9<=10 true).(givennumber(10) % 9== 0 false)
◾ Then i is 10.(10<=10 true).(givennumber(10) % 10== 0 true).then prints 10.
◾ Then i is 11.(11<=10 false).then while loop breaks.
OUTPUT:
4️⃣ COUNT OF DIVISORS OF GIVEN NUMBER:
In the above program we found the divisors.In this program to find count how many divisors of number.
Example:20(1,2,4,5,10,20).This number contains 6 divisors.
STEPS:
✓take the given number 10.so write givennumber=10.
✓Then take the initial value i = 1.
✓need a count so declare the count=0.because we don't know the count.
✓Then give the condition , givennumber is 10, so take the value (i<=10)
✓Next check given number divisible by i,so give(givennumber % i == 0).
✓Need a overall count of givennumber so give count++.It's makes count=count+1.
✓Then increment the value of i.
✓No need to print a i value.Only wanna overall value of count.so print count to declare out of parenthesis.
PROGRAM:
WORKING:
1.First take count=0.
2.Then take givennumber = 10.
3.Next take initial value i=1.
4.Then check the condition , givennumber is 10, so take the value (i<=givennumber).
5.Next check the if condition (givennumber % i == 0).
◾ i is 1.(1<=givennumber true).Then go next step.
(givennumber(10) % 1== 0 true).because the 10 is divided by 1 and the remaider is 0.Condition is true.Then,
6.Increment the value of count++.
7.Then increment the value of i++.
8.print the count.condition is true so, count value is 1.then again go to check while condition.
◾ i is 2.(2<=givennumber true).(givennumber(10) % 2== 0 true).go to next step .count is 2.
◾ i is 3.(3<=givennumber true).(givennumber(10) % 1== 0 false).
◾ i is 4.(4<=givennumber true).(givennumber(10) % 1== 0 false).
◾ i is 5.(5<=givennumber true).(givennumber(10) % 1== 0 true).
count is 3.
◾ i is 6.(6<=givennumber true).(givennumber(10) % 1== 0 false).
◾ i is 7.(7<=givennumber true).(givennumber(10) % 1== 0 false).
◾ i is 8.(8<=givennumber true).(givennumber(10) % 1== 0 false).
◾ i is 9.(9<=givennumber true).(givennumber(10) % 1== 0 false).
◾ i is 10.(10<=givennumber true).(givennumber(10) % 1== 0 true).
count is 4.
◾ i is 11.(11<=givennumber false).then while loop breaks.
OUTPUT:








Top comments (0)