Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding GCD using and LCM of two numbers.cpp #305

Merged
merged 7 commits into from Oct 12, 2020
Merged
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
27 changes: 27 additions & 0 deletions C++/Factorial using Recursion.cpp
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 C++/GCD using Eucledian Algorithm and lcm of two numbers.cpp
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;
}
75 changes: 75 additions & 0 deletions Insertion Sort.cpp
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;


}