Example Program For Find Prime Number Using For Loop In C++


SUBMITTED BY: Nrupeshsinh

DATE: June 24, 2016, 8:27 a.m.

FORMAT: Text only

SIZE: 652 Bytes

HITS: 845

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<math.h> // Math.h For sqrt function
  4. using namespace std;
  5. int main()
  6. {
  7. // Variable Declaration
  8. int n;
  9. // Get Input Value
  10. cout<<"Enter the Number :";
  11. cin>>n;
  12. cout<<"List Of Prime Numbers Below "<<n<<endl;
  13. //for Loop Block For Find Prime Number
  14. for (int i=2; i<n; i++)
  15. for (int j=2; j*j<=i; j++)
  16. {
  17. if (i % j == 0)
  18. break;
  19. else if (j+1 > sqrt(i)) {
  20. cout << i << endl;
  21. }
  22. }
  23. // Wait For Output Screen
  24. getch();
  25. return 0;
  26. }

comments powered by Disqus