Skip to content

Instantly share code, notes, and snippets.

@artulloss
Last active December 18, 2018 15:11
Show Gist options
  • Save artulloss/54f7de7f07098e7e1371d40cdba8245a to your computer and use it in GitHub Desktop.
Save artulloss/54f7de7f07098e7e1371d40cdba8245a to your computer and use it in GitHub Desktop.
A c++ factorial script I wrote in like 5 minutes
#include <iostream>
#include <string>
unsigned long factoral(const unsigned long i) {
if(i == 1)
return 1;
return i * factoral(i - 1);
}
int main()
{
while(true) {
std::cout << "Enter a number to find the factoral, or press q to quit: ";
std::string string;
getline(std::cin, string);
bool onlyDigits = true;
for(char ch : string) {
if(ch == 'q' || ch == 'Q')
return 0;
if(onlyDigits == true && !isdigit(ch))
onlyDigits = false;
}
if(onlyDigits){
std::cout << factoral(stoi(string)) << "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment