Answer to Question #262515 in C++ for Jitendra

Question #262515

Create a class static demo with static member function for following function:


1)find factorial by recursive member function.


2)to check whether a number is prime or not

1
Expert's answer
2021-11-07T14:49:30-0500
#include <iostream>
using namespace std;
class Demo{
    public:
    static int factorial(int n){
        if(n < 2) return 1;
        return n * factorial(n - 1);
    }
    static bool isPrime(int n){
        if(n <= 1) return false;
        else if(n == 2) return true;
        else{
            for(int i = 2; i < n;i++){
                if(n % i == 0) return false;
            }
            return true;
        }
    }
};
int main(){
    cout<<Demo::factorial(4)<<endl;
    cout<<Demo::isPrime(67)<<endl;
    return 0;
}

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
APPROVED BY CLIENTS