From 12fceff3a3b523f8fdb0476ce6b8ce360a545486 Mon Sep 17 00:00:00 2001 From: Thusitha karunathilaka <56513558+Thusiya05@users.noreply.github.com> Date: Wed, 6 Oct 2021 15:24:29 +0530 Subject: [PATCH 1/2] Create Find Factorial --- Find Factorial | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Find Factorial diff --git a/Find Factorial b/Find Factorial new file mode 100644 index 0000000..60f1e3b --- /dev/null +++ b/Find Factorial @@ -0,0 +1,21 @@ +#include +using namespace std; + +int main() { + int n; + long double factorial = 1.0; + + cout << "Enter a positive integer: "; + cin >> n; + + if (n < 0) + cout << "Error! Factorial of a negative number doesn't exist."; + else { + for(int i = 1; i <= n; ++i) { + factorial *= i; + } + cout << "Factorial of " << n << " = " << factorial; + } + + return 0; +} From 8c2ec6e69d00b0f246ff1ea88084d99524ec3726 Mon Sep 17 00:00:00 2001 From: Thusitha karunathilaka <56513558+Thusiya05@users.noreply.github.com> Date: Wed, 6 Oct 2021 15:42:45 +0530 Subject: [PATCH 2/2] Create find the second largest element in an array of n elements --- ... largest element in an array of n elements | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 find the second largest element in an array of n elements diff --git a/find the second largest element in an array of n elements b/find the second largest element in an array of n elements new file mode 100644 index 0000000..d6ab560 --- /dev/null +++ b/find the second largest element in an array of n elements @@ -0,0 +1,42 @@ +#include +using namespace std; +int main(){ + int n, num[50], largest, second; + cout<<"Enter number of elements: "; + cin>>n; + for(int i=0; i>num[i]; + } + /* Here we are comparing first two elements of the + * array, and storing the largest one in the variable + * "largest" and the other one to "second" variable. + */ + if(num[0] largest) { + second = largest; + largest = num[i]; + } + /* If current array element is less than largest but greater + * then second largest ("second" variable) then copy the + * element to "second" + */ + else if (num[i] > second && num[i] != largest) { + second = num[i]; + } + } + cout<<"Second Largest Element in array is: "<