Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Find Factorial
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
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;
}
42 changes: 42 additions & 0 deletions find the second largest element in an array of n elements
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
using namespace std;
int main(){
int n, num[50], largest, second;
cout<<"Enter number of elements: ";
cin>>n;
for(int i=0; i<n; i++){
cout<<"Enter Array Element"<<(i+1)<<": ";
cin>>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]<num[1]){
largest = num[1];
second = num[0];
}
else{
largest = num[0];
second = num[1];
}
for (int i = 2; i< n ; i ++) {
/* If the current array element is greater than largest
* then the largest is copied to "second" and the element
* is copied to the "largest" variable.
*/
if (num[i] > 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: "<<second;
return 0;
}