From 43e122cb9b3c4f559aa3da489d37c3c53b1e63c6 Mon Sep 17 00:00:00 2001 From: Samruddhi Ghodake <72791227+samughodake@users.noreply.github.com> Date: Sat, 2 Oct 2021 01:02:16 +0530 Subject: [PATCH] chore(CPlusPlus): added math factorial (#499) --- algorithms/CPlusPlus/Maths/factorial.cpp | 42 +++++++++++++++ algorithms/CPlusPlus/Maths/missing-number.cpp | 53 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 2 + 3 files changed, 97 insertions(+) create mode 100644 algorithms/CPlusPlus/Maths/factorial.cpp create mode 100644 algorithms/CPlusPlus/Maths/missing-number.cpp 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: "<