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
#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;
}
Comments
Leave a comment