Microsoft Interview Question

Write a program to print out the first n primes.

Interview Answers

Anonymous

Jul 7, 2015

Can you explain your logic? I tried your program in C++, and it even prints 21 and 27 as prime numbers. This is my code, where fmod is a function that calculated the modulus result of 2 decimals (double). int main(){ int n; cin>>n; if(n==0){ return 0; } if(n>=1){ cout1){ if(fmod(x,sqrt(x))!=0){ cout<

1

Anonymous

Jun 1, 2020

The key in generic questions like this, is to make sure to cover the fundamentals. There's usually a back-and-forth with the interviewer. Might be worth doing a mock interview with one of the Microsoft Program Manager Intern experts on Prepfully? Really helps to get some real-world practice and guidance. prepfully.com/practice-interviews

Anonymous

Mar 21, 2014

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

Anonymous

Mar 21, 2014

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