Monday, January 2, 2012

Prime Number


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.
view raw gistfile1.js hosted with ❤ by GitHub

No comments:

Post a Comment