class GeneratePrime{
public static boolean isPrime(int x)
{
for (int i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
public static void main(String args[]){
int currentNumber = 2;
int primesFound = 0;
while (primesFound < 100) {
if (isPrime(currentNumber)) {
primesFound++;
System.out.print (currentNumber + " ");
if (primesFound % 10 == 0) {
System.out.println();
}
}
currentNumber++;
}
}}
Java Interview Question,Java tutorial and Java related Stuffs
Saturday, 18 June 2016
Finding the first 100 prime numbers using array.
Subscribe to:
Post Comments (Atom)
Find Duplicate Characters In String using Java
package com.zia.test; import java.util.HashMap; import java.util.Map; import java.util.Set; public class findDuplicateCharacter { /**...
-
public class Program { public static int getClosestToZero(int[] a) { /* Please implement this method to return the number in...
-
public class FizzBuzzProgramme { public static void main(String[] args) { boolean flag; for (int i = 1; i
No comments:
Post a Comment