InterviewSolution
| 1. |
write a vb program to calculate the sum of all positive even number and sum of all negative odd number from a set of numbers |
|
Answer» java.util.Scanner;public class FiftyPrimes{ // Return true if a number n is PRIME public static boolean isPrime(long n) { // Check DIVISION from 2 to sqrt(n) for (long i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } // If no division is found, number is prime return true; } public static void main(String[] args) { // Create Scanner object Scanner sc = new Scanner(System.in); // Take user input System.out.print("ENTER the starting point: "); long start = sc.nextInt(); // Close Scanner object sc.close(); // If start point is less than 2, make it 2 if (start < 2) { start = 2; } int numberOfPrimes = 0; // Number of primes printed long number = start; // Number to be tested for prime // Iterate until 50 primes are printed while (numberOfPrimes < 50) { if (isPrime(number)) { System.out.println(number); numberOfPrimes++; } number++; } }}The problem STATEMENT is quite clear. First take the user input of a starting point (which we will do with java.util.Scanner) and then print the NEXT 50 primes. |
|