Write C++ program to check whether a number is Prime number or not using while loop


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num, i, f;
  9.  
  10. //Reading a number from user
  11. cout<<"Enter any number:";
  12. cin>>num;
  13.  
  14. f = 0;
  15. i = 2;
  16. while(i <= num/2)
  17. {
  18. if(num%i == 0)
  19. {
  20. f=1;
  21. break;
  22. }
  23. i++;
  24. }
  25. if(f == 0)
  26.  
  27. cout<<num<<" is a Prime Number"<<endl;
  28. else
  29.  
  30. cout<<num<<" is Not a Prime Number"<<endl;
  31.  
  32. return 0;
  33. }
  34.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts