Microsoft

www.microsoft.com
Employer Engaged

Interview Question

Program Manager Intern Interview Redmond, WA

Write a program to print out the first n primes.

Answer

Interview Answer

2 Answers

0

public static void printNPrimes(int n){
      if(n==0){
         return;
      }
      if(n>1){
         System.out.println(2);
         int x=3;
         while(n>0){
             if(x%Math.sqrt(x)!=0){
             System.out.println(x);
             }
             x=x+2;
             n--;
          }
      }
   }

Bharadwaj Tanikella on Mar 20, 2014
0

Fixed.

  public static void printNPrimes(int n){
      if(n==0){
         return;
      }
      if(n>=1){
         System.out.println(2);
         int x=3;
         while(n>1){
             if(x%Math.sqrt(x)!=0){
             System.out.println(x);
             }
             x=x+2;
             n--;
          }
      }
   }

Bharadwaj Tanikella on Mar 20, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.