diff --git a/BinarySearch.cpp b/BinarySearch.cpp index 1e9aa19..c53525f 100644 --- a/BinarySearch.cpp +++ b/BinarySearch.cpp @@ -1,43 +1,32 @@ - #include -using namespace std; - -template -int bSearch(T arr[],int l, int r,T ele) -{ - int mid = (l+r)/2; - if(l ele) - bSearch(arr,l,mid-1,ele); - else if(arr[mid] < ele) - bSearch(arr,mid+1,r,ele); - else - return mid; - } - +using namespace std; +int bin_search(int a[],int n,int num){ + int first=0,last=n-1; + int flag=0; + while(first<=last){ + int mid=(first+last)/2; + if(a[mid]num) + last=mid-1; + else{ + flag=1; + return flag; + } + } + return flag; } - -int main() -{ - int pos,n,key,i; - cout<<"Enter the size of the array : "; - cin>>n; - int arr[n]; - cout<<"Enter the "<>arr[i]; - } - cout<<"Enter the element to be found : "; - cin>>key; - pos = bSearch(arr,0,n-1,key); - if(pos != -1) - cout<<"Element is present at "<>n; + int a[n]; + for(int i=0;i>a[i]; + int num; + cin>>num; + int flag=bin_search(a,n,num); + if(flag==1) + cout<<"Found"; + else cout<<"Not Found"; + return 0; +} diff --git a/BinarySearch.exe b/BinarySearch.exe new file mode 100644 index 0000000..282aeb3 Binary files /dev/null and b/BinarySearch.exe differ diff --git a/BinarySearch.o b/BinarySearch.o new file mode 100644 index 0000000..9223875 Binary files /dev/null and b/BinarySearch.o differ diff --git a/LeapYear.cpp b/LeapYear.cpp index 70154f6..37cecdb 100644 --- a/LeapYear.cpp +++ b/LeapYear.cpp @@ -1,31 +1,15 @@ #include using namespace std; - -int main() - { - int year; - - cout<<"Program to Verify Leap Year...\n"; - - cout << "Enter a Year: "; - cin >> year; - - if (year % 4 == 0) - { - if (year % 100 == 0) - { - if (year % 400 == 0) - cout << "Year " << year << " is a Leap Year."; - - else - cout << "Year " << year << " is NOT a Leap Year."; - } - else - cout << "Year " << year << " is a Leap Year."; - } - else - cout << "Year " << year << " is not a Leap Lear."; - - return 0; - - } +int main(){ + int yr; + cin>>yr; + if(yr%4==0 && yr%100==0){ + if(yr%400==0) + cout<<"Leap Year"; + else cout<<"Not leap year"; + } + if(yr%4==0 && yr%100!=0){ + cout<<"Leap Year"; + } + return 0; +} diff --git a/LeapYear.exe b/LeapYear.exe new file mode 100644 index 0000000..6e25668 Binary files /dev/null and b/LeapYear.exe differ diff --git a/LeapYear.o b/LeapYear.o new file mode 100644 index 0000000..3c6956f Binary files /dev/null and b/LeapYear.o differ diff --git a/Palindrome.cpp b/Palindrome.cpp index b4209e8..cc8e4be 100644 --- a/Palindrome.cpp +++ b/Palindrome.cpp @@ -1,35 +1,17 @@ -/* - The following code checks if the given number is a palindrome or not. -*/ - - -#include - -using namespace std; - -int main() -{ - - // o --> original number - // r --> reversed number - - int n, r= 0, remainder, o; - cout<<"Enter an number: "; - cin>>n; - o = n; - - while (n != 0) - { - remainder = n % 10; - r = r * 10 + remainder; - n /= 10; - } - - - if (o == r) - cout< +using namespace std; +int main(){ + int num; + cin>>num; + int save=num; + int sum=0; + while(num!=0){ + int t=num%10; + sum=10*sum+t; + num/=10; + } + if(save==sum) + cout<<"Palindrome"; + else cout<<"Not Palindrome"; + return 0; +} diff --git a/Palindrome.exe b/Palindrome.exe new file mode 100644 index 0000000..2d84a3c Binary files /dev/null and b/Palindrome.exe differ diff --git a/Palindrome.o b/Palindrome.o new file mode 100644 index 0000000..f38813c Binary files /dev/null and b/Palindrome.o differ diff --git a/README.md b/README.md index 2690abf..65e73b1 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ # Cpp-Programs +This repository contains all the basic c++ programs. \ No newline at end of file diff --git a/Reverse a string.cpp b/Reverse a string.cpp index de585db..162d3de 100644 --- a/Reverse a string.cpp +++ b/Reverse a string.cpp @@ -1,12 +1,9 @@ #include using namespace std; -int main() -{ - string str = "string"; - - - reverse(str.begin(), str.end()); - - cout << str; +int main(){ + string s; + getline(cin,s); + reverse(s.begin(),s.end()); + cout< #include using namespace std; - -int main() -{ - - int arr[] = { 1, 45, 54, 71, 76, 12 }; - - - int n = sizeof(arr) / sizeof(arr[0]); - - - cout << "Array: "; - for (int i = 0; i < n; i++) - cout << arr[i] << " "; - - - reverse(arr, arr + n); - - - cout << "\nReversed Array: "; - for (int i = 0; i < n; i++) - cout << arr[i] << " "; +int main(){ + int n; + cin>>n; + int a[n]; + for(int i=0;i>a[i]; + for(int i=n-1;i>=0;i--) + cout< using namespace std; - -int main() -{ - int i,j, m, n; - cout<<"\nEnter number of rows and coloumns : "; - cin>>m>>n; - int arr[m][n]; - cout<<"\nEnter elements of matrix : \n"; - for (i = 0; i < m; i++) - for (j = 0; j < n; j++) - cin>>arr[i][j]; - - cout << "\nFinding Sum of each row:"; - int sum=0; - - for (i = 0; i < m; ++i) {s - for (j = 0; j < n; ++j) { - sum = sum + arr[i][j]; - } - cout<< "\nSum of the row "<< i << " = " << sum << endl; - sum = 0; - } - +int main(){ + int m,n; + cin>>m>>n; + int a[m][n]; + for(int i=0;i>a[i][j]; + } + int sum=0; + int i=0; + for(int i=0;i -using namespace std; - -int main(){ - int num1=0,num2=1,num3,i,n; - cout<<"\nEnter the number of elements of Fibonacci Series : "; - cin>>n; - cout< +using namespace std; +int fib(int N){ + int a=0,b=1; + int sum=0; + cout<>n; + fib(n-2); + return 0; +} diff --git a/fibonacci.exe b/fibonacci.exe new file mode 100644 index 0000000..33983ce Binary files /dev/null and b/fibonacci.exe differ diff --git a/fibonacci.o b/fibonacci.o new file mode 100644 index 0000000..b7ee0cf Binary files /dev/null and b/fibonacci.o differ diff --git a/kthLargestElement.cpp b/kthLargestElement.cpp index 1d3f355..e026b00 100644 --- a/kthLargestElement.cpp +++ b/kthLargestElement.cpp @@ -1,32 +1,32 @@ -#include - -using namespace std; - -int main() -{ - int a[50] , n , temp, k , i , j; - cout<<"Enter the size of the array : "; - cin>>n; - cout<<"Enter the value of k : "; - cin>>k; - for(i=0;i>a[i]; - } - - for(i=0;i a[j+1]) - { - temp = a[j]; - a[j] = a[j+1]; - a[j+1] = temp; - } - } - } - cout< +using namespace std; +int get_max(int a[], int n) { + int maxm=INT_MIN; + for (int i = 0; i < n; i++) { + maxm = max(maxm, a[i]); + } + for(int i=0;i> n; + int a[n]; + for (int i = 0; i < n; i++) + cin >> a[i]; + int k; + cout << "which largest element:"; + cin >> k; + for (int i = 1; i <= k - 1; i++) { + int t = get_max(a, n); + a[t]=INT_MIN; + } + int maxm = INT_MIN; + for (int i = 0; i < n; i++) { + maxm = max(maxm, a[i]); + } + cout << endl; + cout << maxm; +} diff --git a/kthLargestElement.exe b/kthLargestElement.exe new file mode 100644 index 0000000..aa8fb86 Binary files /dev/null and b/kthLargestElement.exe differ diff --git a/kthLargestElement.o b/kthLargestElement.o new file mode 100644 index 0000000..47a3162 Binary files /dev/null and b/kthLargestElement.o differ