Saturday, 18 June 2016

Finding the first 100 prime numbers using array.

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++;
    }
}}


No comments:

Post a Comment

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 { /**...