Description
Write a java program to find the sum of all the prime numbers less then a given natural number N. The main purpose of this inteview question is to check the programming sense and capabilities to check how good you are to convert existing logic into code. The question is mostly asked to freshers.The only main logic in this program is to find the prime numbers.Prime number is a number that is greater than 1 and divided by 1 or itself.For reminder, 2 is also a prime number.
PrimeNumberSum.javapackage com.devglan; public class PrimeNumberSum { public long sum(int limit){ int number = 2; int count = 0; long sum = 0; while(count < limit){ if(isPrimeNumber(number)){ sum += number; count++; } number++; } return sum; } private boolean isPrimeNumber(int number){ for(int i=2; i<=number/2; i++){ if(number % i == 0){ return false; } } return true; } public static void main(String args[]){ PrimeNumberSum primeNumberSum = new PrimeNumberSum(); System.out.println(primeNumberSum.sum(1000)); } }