C++ Implement a number class to represent integers in accordance with the follow
ID: 3938436 • Letter: C
Question
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.
Explanation / Answer
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.