forked from aliya-rahmani/Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aliya-rahmani#305 from SivaKumar2001/SivaKumar2001…
…-patch-1 Finding GCD using and LCM of two numbers.cpp
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
//Time Complexity - O(N) | ||
|
||
int Recursive_Function(int a) | ||
{ | ||
if(a==0) | ||
{ | ||
return 1; | ||
} | ||
else | ||
{ | ||
return (a*Recursive_Function(a-1)); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int N; | ||
cout<<"Enter the value of N:"; | ||
cin >>N; | ||
int Factorial = Recursive_Function(N); | ||
cout<<"Factorial of "<<N<<" is "<<Factorial; | ||
return 0; | ||
} |
37 changes: 37 additions & 0 deletions
37
C++/GCD using Eucledian Algorithm and lcm of two numbers.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
//Time complexity - O(log N) | ||
|
||
//GCD | ||
int GCD(int x,int y) | ||
{ | ||
if (x==0) | ||
{ | ||
return y; | ||
} | ||
return GCD(y%x,x); | ||
} | ||
|
||
//LCM | ||
int LCM(int x1,int y1) | ||
{ | ||
int z = GCD(x1,y1); | ||
int lcm = (x1*y1)/z; | ||
return lcm; | ||
} | ||
|
||
|
||
|
||
int main() | ||
{ | ||
int a,b; | ||
cout<<"Enter the value of a and b:"; | ||
cin>>a>>b; | ||
int c = GCD(a,b); | ||
int lcm_of_two_numbers = LCM(a,b); | ||
cout<<c<<endl; | ||
cout<<lcm_of_two_numbers<<endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
//Insertion Sort in the ascending order | ||
|
||
/*Performing Insertion Sort - We take first element in the array as sorted sublist | ||
others are unsorted sublist and first element in the unsorted sublist, | ||
we take it as temporary value and we compare it with all elements in the sorted sublist | ||
if temporary value is less than last element in the sorted list we move that last element to | ||
the temporary value with in that we take it as sorted sublist and we insert temp value in the sorted sublist*/ | ||
|
||
int Insertion_Sort(int arr[],int n) | ||
{ | ||
for(int i=1;i<n;i++) | ||
{ | ||
//temporary value to perform insertion sort | ||
int temp = arr[i]; | ||
|
||
//limit to check all the elements in the sorted sublist | ||
int j = i-1; | ||
|
||
//It checks the elements in the sorted sublist if last value in the sorted sublist is less than temp then it enters in to the loop | ||
while(j>=0 && arr[j]>temp) | ||
{ | ||
//To move the value which is greater than temp value | ||
arr[j+1] = arr[j]; | ||
|
||
//Decrementing to check all the elements in the sorted sublist | ||
j--; | ||
} | ||
|
||
//If while loop condition(arr[j] > temp) fails then it take till temp as sorted sublist | ||
arr[j+1] = temp; | ||
} | ||
|
||
//Printing all the elements in the array After performing Insertion Sort | ||
cout<<"\nAfter performing Insertion Sort\n"; | ||
for(int j=0;j<n;j++) | ||
{ | ||
cout<<arr[j]<<" "; | ||
} | ||
cout<<"\n"; | ||
|
||
} | ||
|
||
|
||
int main() | ||
{ | ||
int arr[50],size; | ||
cout<<"Enter the size of array:\n"; | ||
cin>>size; | ||
|
||
//Array input | ||
for(int i=0;i<size;i++) | ||
{ | ||
cin>>arr[i]; | ||
} | ||
|
||
//Printing all the elements in the array Before performing Insertion Sort | ||
cout<<"\nBefore performing Insertion Sort\n"; | ||
for(int j=0;j<size;j++) | ||
{ | ||
cout<<arr[j]<<" "; | ||
} | ||
|
||
//for newline | ||
cout<<"\n"; | ||
|
||
Insertion_Sort(arr,size); | ||
|
||
return 0; | ||
|
||
|
||
} |