Example Program For Simple Class Example Program For Prime Number In C++


SUBMITTED BY: Nrupeshsinh

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

FORMAT: Text only

SIZE: 695 Bytes

HITS: 769

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. // Class Declaration
  5. class prime
  6. {
  7. //Member Varibale Declaration
  8. int a,k,i;
  9. public:
  10. prime(int x)
  11. {
  12. a=x;
  13. }
  14. // Object Creation For Class
  15. void calculate()
  16. {
  17. k=1;
  18. {
  19. for(i=2;i<=a/2;i++)
  20. if(a%i==0)
  21. {
  22. k=0;
  23. break;
  24. }
  25. else
  26. {
  27. k=1;
  28. }
  29. }
  30. }
  31. void show()
  32. {
  33. if(k==1)
  34. cout<<"\n"<<a<<" is Prime Number.";
  35. else
  36. cout<<"\n"<<a<<" is Not Prime Numbers.";
  37. }
  38. };
  39. //Main Function
  40. int main()
  41. {
  42. int a;
  43. cout<<"Enter the Number:";
  44. cin>>a;
  45. // Object Creation For Class
  46. prime obj(a);
  47. // Call Member Functions
  48. obj.calculate();
  49. obj.show();
  50. getch();
  51. return 0;
  52. }

comments powered by Disqus