chore(CPlusPlus): added math factorial (#499)
parent
a6f9448ef9
commit
43e122cb9b
|
@ -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 <iostream>
|
||||||
|
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: "<<factorial(n);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Input:
|
||||||
|
Enter a number:
|
||||||
|
3
|
||||||
|
|
||||||
|
Output:
|
||||||
|
factorial is: 6
|
||||||
|
*/
|
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
Description: Given an vector v containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the vector.
|
||||||
|
|
||||||
|
Approach: Using for loop from 0 to n and searching if the value is present in the vector or not using find() function.
|
||||||
|
If a certain value is not present, the function will return it.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//function starts
|
||||||
|
int missingNumber(vector<int> &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<int> v(n);
|
||||||
|
cout<<"Enter any "<<n<<" elements (From 0 to "<<n<<") \n";
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
cin>>v[i];
|
||||||
|
}
|
||||||
|
cout<<"\nMissing number is: "<<missingNumber(v);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Input:
|
||||||
|
Enter number of elements:
|
||||||
|
4
|
||||||
|
Enter any 4 elements (From 0 to 4)
|
||||||
|
0
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
|
||||||
|
Output:
|
||||||
|
Missing number is: 4
|
||||||
|
*/
|
|
@ -126,6 +126,8 @@
|
||||||
5. [Armstrong Number](Maths/armstrong.cpp)
|
5. [Armstrong Number](Maths/armstrong.cpp)
|
||||||
6. [Palindrome](Maths/palindrome.cpp)
|
6. [Palindrome](Maths/palindrome.cpp)
|
||||||
7. [Reverse digit of a number](Maths/reverse-digits.cpp)
|
7. [Reverse digit of a number](Maths/reverse-digits.cpp)
|
||||||
|
8. [Missing number](Maths/missing-number.cpp)
|
||||||
|
9. [Factorial of a number](Maths/factorial.cpp)
|
||||||
|
|
||||||
# Recursion
|
# Recursion
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue