diff --git a/2D_vector.cpp b/2D_vector.cpp new file mode 100644 index 0000000..42cbfe4 --- /dev/null +++ b/2D_vector.cpp @@ -0,0 +1,31 @@ +// +// Created by ankit on 12-09-2019. +// + +#include +using namespace std; + +int main() +{ + int row, col; + cin >> row >> col; + vector< vector >v; + v.resize(col,vector(row)); + + for (int i = 0; i < row; i++) + { + for (int j = 0; j < col; j++) + { + cin >> v[i][j]; + } + } + + for (int i = 0; i < row; i++) + { + for (int j = 0; j < col; j++) + { + cout << v[i][j] << " "; + } + cout << endl; + } +} \ No newline at end of file diff --git a/Age_Calculator.cpp b/Age_Calculator.cpp index 6be2102..369b8b8 100644 --- a/Age_Calculator.cpp +++ b/Age_Calculator.cpp @@ -1,26 +1,25 @@ //Program to check your age till date -#include -#include -void main() +#include +using namespace std; +int main() { -int birth_month,birth_year; -int current_month,current_year; -int ageinyear,ageinmonth; -cout<<"Age Calculator\n\n"; + int birth_month, birth_year; + int current_month, current_year; + int ageinyear, ageinmonth; + cout << "Age Calculator\n\n"; -cout<<"\nEnter Your Birth Month(Eg:8): "; -cin>>birth_month; -cout<<"\nEnter Your Birth Year(Eg:1999): "; -cin>>birth_year; + cout << "\nEnter Your Birth Month(Eg:8): "; + cin >> birth_month; + cout << "\nEnter Your Birth Year(Eg:1999): "; + cin >> birth_year; -cout<<"\nEnter The Current Month(Eg:12): "; -cin>>current_month; -cout<<"\nEnter The Current Year(Eg:2020): "; -cin>>current_year; -ageinyear= ; -ageinmonth= ; -cout<<"\n\nYour Age is "<> current_month; + cout << "\nEnter The Current Year(Eg:2020): "; + cin >> current_year; + ageinyear = current_year - birth_year; + ageinmonth = 12 - birth_month; + cout << "\n\nYour Age is " << ageinyear << " Years And " << ageinmonth << " Months "; } diff --git a/Calculator.cpp b/Calculator.cpp index aa82b4f..c37e743 100644 --- a/Calculator.cpp +++ b/Calculator.cpp @@ -1,10 +1,19 @@ //Basic Calculator using C++ # include +#include using namespace std; + +template +T find_max(T a, U b) +{ + return a > b ? a : b; +} int main() { - int ch,sum; + int ch; + float sum, sub, mul, div,max; + int mod; float num1, num2; cout << "Enter option \n 1.Addition \t 2. Subtraction \n 3.Multiplication \t 4. Division \n 5. Modulus \t 6. Maximum of two nos \n "; cin >> ch; @@ -13,9 +22,35 @@ int main() switch(ch) { case 1: - sum=num1+num2; - cout<<"Sum of two numbers is "<= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')): + print("The Given Character ", ch, "is an Alphabet") +elif(ch >= '0' and ch <= '9'): + print("The Given Character ", ch, "is a Digit") +else: + print("The Given Character ", ch, "is a Special Character") diff --git a/Seriesplay.cpp b/Seriesplay.cpp index 820932c..e6f81d3 100644 --- a/Seriesplay.cpp +++ b/Seriesplay.cpp @@ -1,4 +1,4 @@ -//Print the following Series using a menu driven program +//Print the following Series using a menu driven program //1. Fibonacci series //2. n natural numbers //3. n prime numbers @@ -10,27 +10,170 @@ //7. 1+2+6+24+120+..... //8. 1/2+2/3+3/4+4/5+.... -# include +#include using namespace std; + +int fib(int n) +{ + if (n == 0 || n == 1) + return 1; + else + return fib(n - 1) + fib(n - 2); +} + +void natural(int n) +{ + for (int i = 1; i <= n; ++i) + { + cout << i << " "; + } + cout << endl; +} + +bool isPrime(int n) +{ + if (n <= 1) + return false; + for (int i = 2; i < n; i++) + if (n % i == 0) + return false; + + return true; +} +void printPrime(int n) +{ + for (int i = 2; i <= n; i++) { + if (isPrime(i)) + cout << i << " "; + } +} + +void even_number(int n) +{ + for (int i = 0; i <= n; i = i + 2) + { + cout << i << " "; + } + cout << endl; +} + +int createPalindrome(int input, int b, bool isOdd) +{ + int n = input; + int palin = input; + if (isOdd) + n /= b; + while (n > 0) + { + palin = palin * b + (n % b); + n /= b; + } + return palin; +} +void generatePalindromes(int n) +{ + int number; + for (int j = 0; j < 2; j++) + { + int i = 1; + while ((number = createPalindrome(i, 10, j % 2)) < n) + { + cout << number << " "; + i++; + } + } +} + +void square_sum(int n) +{ + int sum = 0; + for (int i = 1; i <= n; ++i) + { + sum += i * i; + } + cout << sum << endl; +} + + +int fact(int n) +{ + if (n <= 0) + return 1; + else + return fact(n - 1) * n; +} + +int sum_fact(int n) +{ + int sum1 = 0; + for (int i = 1; i <= n; ++i) + { + sum1 += fact(i); + } + cout << sum1 << endl; +} + +int series(int n) +{ + int a = 1; + float sum_series = 0; + for (int i = 0; i < n; ++i) + { + sum_series += (float) a / (a + 1); + a++; + } + cout << sum_series << endl; +} int main() { int ch; - + cout << "Enter option \n"; cout<<"1. Print Fibonacci series\n"; cout<<"2. n natural numbers\n"; cout<<"3. n prime numbers\n"; cout<<"4. n even numbers\n"; cout<<"5. n pallindrome numbers\n"; - cout<<"6. 1+4+9+16+.....n"; + cout<<"6. 1+4+9+16+.....\n"; cout<<"7. 1+2+6+24+120+.....\n"; cout<<"8. 1/2+2/3+3/4+4/5+....\n"; cin >> ch; - + int n1; + cin >> n1; switch(ch) { case 1: - + cout << fib(n1); + break; + + case 2: + natural(n1); + break; + + case 3: + printPrime(n1); + break; + + case 4: + even_number(n1); + break; + + case 5: + generatePalindromes(n1); + break; + + case 6: + square_sum(n1); + break; + + case 7: + sum_fact(n1); + break; + + case 8: + series(n1); + break; + default: // If the options is not 1-8, error message is shown cout << "Error! Option is not correct"; diff --git a/binary_search1.cpp b/binary_search1.cpp new file mode 100644 index 0000000..75ecdee --- /dev/null +++ b/binary_search1.cpp @@ -0,0 +1,34 @@ +// +// Created by ankit on 07-08-2019. +// + +#include +using namespace std; + +typedef int ll; + +int main(void) +{ + ll n, x; + cin >> n >> x; + vector v; + ll input; + for (int i = 0; i < n; i++) + { + cin >> input; + v.push_back(input); + } + ll k = 0; + for (int b = n / 2; b >= 1; b /= 2) + { + while (k + b < n && v[k + b] <= x) + k += b; + } + + if (v[k] == x) + { + cout << "Element found at " << k + 1 << endl; + } + else + cout << "No element found" << endl; +} \ No newline at end of file diff --git a/circular_rotation.cpp b/circular_rotation.cpp new file mode 100644 index 0000000..0d3dfe7 --- /dev/null +++ b/circular_rotation.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +int main() +{ + int n, k, q; + cin >> n >> k >> q; + + deque v; + int input; + for (int i = 0; i < n; i++) + { + cin >> input; + v.push_back(input); + } + for (int i = 0; i < k; i++) + { + v.push_front(v[n - 1]); + } + + for (int i = 0; i < q; i++) + { + int m; + cin >> m; + cout << v[m] << endl; + } +} diff --git a/jump_search.cpp b/jump_search.cpp new file mode 100644 index 0000000..79dc0e1 --- /dev/null +++ b/jump_search.cpp @@ -0,0 +1,59 @@ +// +// Created by ankit on 15-07-2019. +// + +#include +using namespace std; + +typedef long long int lli; + +int jumpSearch (vector v, lli n, lli x) { + lli step = sqrt(n); + + lli prev = 0; + + while (v[min(step, n) - 1] < x) + { + prev = step; + step += sqrt(n); + + if (prev >= n) + return -1; + } + + while (v[prev] < x) + { + prev++; + + if (prev == min(step, n)) + return -1; + } + + if (v[prev] == x) + return prev; + + return -1; +} + +int main() +{ + lli t; + cin >> t; + + for (int i = 0; i < t; i++) + { + lli n; + cin >> n; + vector v; + lli input; + + for (int i = 0; i < n; i++) + { + cin >> input; + v.push_back(input); + } + lli k; + cin >> k; + cout << jumpSearch(v, n, k) << endl; + } +}