This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is simple javascript program to find out Prime Numbers between 1-100 and then store into empty array. | |
function prime(i) | |
{ | |
var j; | |
if(i==0 || i==1) { // zero or one are not primes so they shd be exluded. | |
return false; | |
} | |
for(j=2; j | |
{ | |
if(i%j==0) // if reminder is zero then false. in loop beacuse check for all value 1-10. | |
{ | |
return false; // otherwise return true. | |
} | |
} | |
return true; | |
} | |
function main() // main function which gives all prime bumber into an array. | |
{ | |
var arr = new Array(); | |
var result; | |
for(i=0; i<100; i++) | |
{ | |
result = prime(i); // get the returned value by fucntion into variable result. | |
if(result == true) | |
{ | |
arr.push(i); // all true values push or add into empty array. | |
} | |
} | |
console.log(arr); | |
} | |
explanation : in this use of two function main and prime . main function is to get all the prime numbers from 1- 100 and store into a empty array using push method of array . push method is to store elements into array. |
No comments:
Post a Comment