Problem 26. prime.js ( using mark variable)

 /* Question

How to find Prime (steps)
eg:- 7 is divisible by 1,7
which means it is not divisible 2,3,4,5,6

Algorithm:-
1. Read the number
2. Run a loop from 2 to number -1 (eg 7-1 =6)
3. Check if the number is divisible by any number in the above range
4. If yes , Not Prime else Prime
*/


let num = 7;

let mark = true // we are assuming that the number is prime in the beginning

let i = 2;

while (i < num) {
if (num % i == 0) {
mark = false; // since number is divisble threfore mark becomes false

console.log('Not Prime');

break;
}

i++;

}

if (mark == true) {
console.log('Prime');
}

OR=====================================================================

let num = 7;

let mark = true // we are assuming that the number is prime in the beginning

let i = 2;

while (i < num) {
if (num % i == 0) {
mark = false; // since number is divisble threfore mark becomes false

break;
}

i++;

}

if (mark == true) {
console.log('Prime');
}

else {
console.log('Not Prime')
}

Termianal:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node prime.js Prime Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$


Comments

Popular posts from this blog

Problem 33. patter122333.js

Problem 38. diamond.js

Problem 16. chocolatebills.js