diff --git a/algorithms/CPlusPlus/Maths/factorial.cpp b/algorithms/CPlusPlus/Maths/factorial.cpp new file mode 100644 index 00000000..11ad4773 --- /dev/null +++ b/algorithms/CPlusPlus/Maths/factorial.cpp @@ -0,0 +1,42 @@ +/* +Description: A program to calculate factorial of a number. +A factorial of number 4 is calculated as: +4 X 3 X 2 X 1 = 24 + +Approach: Calculating factorial using for loop. +Declaring the f varialbe to 1 (not initialising it to zero because any number multiplied by 0 will be 0) +Multiplying the f variable to 1,2,3...n and storing it in the f varialbe. +The same factorial can be calculated using while loop, recursion. + +Time Complexity: O(number) +*/ + +#include +using namespace std; + +//function starts +long factorial (long n){ + long f=1; + for(long i=1;i<=n;i++){ + f=f*i; + } + return f; +} + +//main starts +int main() { + cout << "Enter a number: \n"; + long n; + cin>>n; + cout<<"factorial is: "< +#include +#include +using namespace std; + +//function starts +int missingNumber(vector &v){ + int num; + for(int i=0;i<=v.size();i++){ + //checking if i is present or not in the vector + //if not present,it will store the value of i in the num variable and breaking it + if(find(v.begin(),v.end(),i)==v.end()){ + num=i; + break; + } + } + return num; +} + +//main starts +int main() { +cout << "Enter number of elements:\n"; +int n; +cin>>n; +vector v(n); +cout<<"Enter any "<>v[i]; +} +cout<<"\nMissing number is: "<