Last active
May 18, 2017 12:00
-
-
Save m-ahmedy/18e32c97bbc9d674f8fc5717db9266c3 to your computer and use it in GitHub Desktop.
A function that checks whether a number is prime or not + Factorial function with recursion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
bool isPrime(int N){ | |
int divider; | |
for(divider=N-1 ; divider>1 && N%divider!=0 ; divider--); | |
if(divider==1)return true; | |
else return false; | |
} | |
int main() | |
{ | |
int num; | |
cout<<"Enter an integer: "; | |
cin>>num; | |
if( isPrime(num) )cout<<num<<" is a prime number\n"; | |
else cout<<num<<" is not a prime number\n"; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
double factorial(int N) | |
{ | |
if(N==0 || N==1) | |
return 1; | |
else | |
return N*factorial(N-1); | |
} | |
int main() | |
{ | |
int num; | |
do | |
{ | |
cout<<"Enter a positive number to calculate its factorial: "; | |
cin>>num; | |
} | |
while(num<0); | |
cout<<"The factorial of "<<num<<" = " <<factorial(num)<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment