Ad Code

Responsive Advertisement

4) Write a program to check whether a number is prime or not by overloading – operator.










//
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class A{
 int a;
 public:

 void set_a(int a){
  this->a=a;
 } 
 int get_a(){
  return a;
 }
};
A operator --(A obj){
 int j=0;
 //cout<<obj.get_a();
 for(int i=1; i<=obj.get_a(); i++){
  if(obj.get_a()%i==0){
   j++;
  }
 }
 if(j==2){
  cout<<"\n\nIt is a prime number\n\n";
 }
 else{cout<<"\n\nIt is a not prime number\n\n";}
}
int main(){
 A obj;
 int num;
 system("clear");
 cout<<"Enter a number :";
 cin>>num;
 obj.set_a(num);
 --obj;
}



out put:
 Enter a number 11
  It is a prime number


If you like code with vibhu  and would like to contribute, you can also write an article using   This link :

https://docs.google.com/forms/d/e/1FAIpQLScAmvlPvNUz35R-G0nc_zpRVP3o8xlhtFgC3aKPyLetX_RyXg/viewform?pli=1


OR  mail your article to codewithvibhu@gmail.com.  See your article appearing on the code with vibhu main page and help other students. 

Post a Comment

0 Comments