C Implement a number class to represent integers in accordan

C++

Implement a number class to represent integers in accordance with the following UML class diagram.

The number class represents integers. The integer value it represents is passed into the constructor and stored in member variable n. The setValue function takes an integer argument, which it copies into it\'s private member variable n. The isPrime function returns true if member variable n is prime and false if not prime. The isLucky function returns true if the member variable n is divisible by 7.

Solution

ANS:

#include <iostream>
#include<assert.h>
using namespace std;
class Number
{
private: int n;
public: Number(int n)//constructor
{
this->n=n;
}
void setValue(int n)
{
this->n=n;
}
bool isPrime(){
int i,count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}
bool isLucky(){
  
if(n%7 == 0)
return true;
else
return false;
}
  
};
int main()
{
Number p(17);
int n;
cout<<\"enter n\";
cin>>n;
p.setValue(n);
bool b=p.isPrime();
assert(b == true && \"Not a Prime Number\");
cout<<\"Prime Number\"<<endl;
bool b1=p.isLucky();
assert(b1 == true && \"The Number variable n is not divisible by 7\");
cout<<\"the member variable n is divisible by 7\"<<endl;

return 0;
}

C++ Implement a number class to represent integers in accordance with the following UML class diagram. The number class represents integers. The integer value i
C++ Implement a number class to represent integers in accordance with the following UML class diagram. The number class represents integers. The integer value i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site