Answer to Question #293935 in C++ for Param

Question #293935

Write a C++ program to execute the following the logic:

Create a class named with MagicNumber, having one data member like No and having different member function to perform the unique task. All the member functions are must be as argument based function.


GetNo(),PrintNo(int),FactNo(int),PatternNo(int),PrimeNo(int),SquareNo(int).


  • GetNo function will get the any positive number from user.

  • If no is >10 than call the SquareNo function and find the square of that no.

  • If no >5 and <10 than call FactNo function and find the factorial of that no.

  • If no<5 then call the PatternNo function and display any pattern based on that no.

  • If given number is odd than call the PrimeNo function and check given no is prime or not.


1
Expert's answer
2022-02-04T07:59:13-0500
#include <iostream>
using namespace std;


class MagicNumbers {
    int No;


public:
    void GetNo();
    void PrintNo(int x);
    int SquareNo(int x);
    int FactNo(int x);
    void PatternNo(int x);
    bool PrimeNo(int x);
};


void MagicNumbers::GetNo()
{
    cout << "Get any positive number: ";
    cin >> No;
    if ( No <= 0 ) {
        cout << "Incorrect input" << endl;
        return;
    }


    if (No%2 == 1) {
        if (PrimeNo(No)) {
            cout << No << " is a prime number" << endl;
        }
        else {
            cout << No << " is a composite  number" << endl;
        }
    }


    if (No > 10) {
        No = SquareNo(No);
        PrintNo(No);
    }
    if (No > 5 && No < 10) {
        No = FactNo(No);
        PrintNo(No);
    }
    if (No < 5) {
        PatternNo(No);
    }
}


void MagicNumbers::PrintNo(int x) {
    cout << "The number is " << x << endl;
}


int MagicNumbers::SquareNo(int x) {
    return x*x;
}


int MagicNumbers::FactNo(int x) {
    int res = 1;
    for (int i=2; i<=x; i++) {
        res *= i;
    }
    return res;
}


void MagicNumbers::PatternNo(int x) {
    for (int i=0; i<x; i++) {
        for (int j=0; j<x; j++) {
            cout << "*";
        }
        cout << endl;
    }
}


bool MagicNumbers::PrimeNo(int x) {
    if (x%2 == 0 && x != 2) {
        return false;
    }


    int d = 3;
    while ( x > d*d) {
        if (x%d == 0) {
            return false;
        }
        d += 2;
    }
    return true;
}


int main() {
    MagicNumbers mn;


    mn.GetNo();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog