diff --git a/Armstrong b/Armstrong new file mode 100644 index 000000000..932c42741 --- /dev/null +++ b/Armstrong @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() { + int num, originalNum, remainder, result = 0; + cout << "Enter a three-digit integer: "; + cin >> num; + originalNum = num; + + while (originalNum != 0) { + // remainder contains the last digit + remainder = originalNum % 10; + + result += remainder * remainder * remainder; + + // removing last digit from the orignal number + originalNum /= 10; + } + + if (result == num) + cout << num << " is an Armstrong number."; + else + cout << num << " is not an Armstrong number."; + + return 0; +} diff --git a/Armstrongnumber b/Armstrongnumber new file mode 100644 index 000000000..932c42741 --- /dev/null +++ b/Armstrongnumber @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() { + int num, originalNum, remainder, result = 0; + cout << "Enter a three-digit integer: "; + cin >> num; + originalNum = num; + + while (originalNum != 0) { + // remainder contains the last digit + remainder = originalNum % 10; + + result += remainder * remainder * remainder; + + // removing last digit from the orignal number + originalNum /= 10; + } + + if (result == num) + cout << num << " is an Armstrong number."; + else + cout << num << " is not an Armstrong number."; + + return 0; +} diff --git a/Avenger b/Avenger new file mode 100644 index 000000000..9299b76dc --- /dev/null +++ b/Avenger @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main() { + int i, n; + bool isPrime = true; + + cout << "Enter a positive integer: "; + cin >> n; + + // 0 and 1 are not prime numbers + if (n == 0 || n == 1) { + isPrime = false; + } + else { + for (i = 2; i <= n / 2; ++i) { + if (n % i == 0) { + isPrime = false; + break; + } + } + } + if (isPrime) + cout << n << " is a prime number"; + else + cout << n << " is not a prime number"; + + return 0; +} diff --git a/Binary search tree b/Binary search tree new file mode 100644 index 000000000..823695cc6 --- /dev/null +++ b/Binary search tree @@ -0,0 +1,359 @@ +# include +# include +using namespace std; +struct nod//node declaration +{ + int info; + struct nod *l; + struct nod *r; +}*r; +class BST +{ + public://functions declaration + void search(nod *, int); + void find(int, nod **, nod **); + void insert(nod *, nod *); + void del(int); + void casea(nod *,nod *); + void caseb(nod *,nod *); + void casec(nod *,nod *); + void preorder(nod *); + void inorder(nod *); + void postorder(nod *); + void show(nod *, int); + BST() + { + r = NULL; + } +}; +void BST::find(int i, nod **par, nod **loc)//find the position of the item +{ + nod *ptr, *ptrsave; + if (r == NULL) + { + *loc = NULL; + *par = NULL; + return; + } + if (i == r→info) + { + *loc = r; + *par = NULL; + return; + } + if (i < r→info) + ptr = r→l; + else + ptr = r→r; + ptrsave = r; + while (ptr != NULL) + { + if (i == ptr→info) + { + *loc = ptr; + *par = ptrsave; + return; + } + ptrsave = ptr; + if (i < ptr→info) + ptr = ptr→l; + else + ptr = ptr→r; + } + *loc = NULL; + *par = ptrsave; +} +void BST::search(nod *root, int data) //searching +{ + int depth = 0; + nod *temp = new nod; + temp = root; + while(temp != NULL) + { + depth++; + if(temp→info == data) + { + cout<<"\nData found at depth: "< data) + temp = temp→l; + else + temp = temp→r; + } + cout<<"\n Data not found"< newnode→info) + { + if (tree→l != NULL) + { + insert(tree→l, newnode); + } + else + { + tree→l= newnode; + (tree→l)→l = NULL; + (tree→l)→r= NULL; + cout<<"Node Added To Left"<>c; + switch(c) + { + case 1: + t = new nod; + cout<<"Enter the number to be inserted : "; + cin>>t→info; + bst.insert(r, t); + break; + case 2: + if (r == NULL) + { + cout<<"Tree is empty, nothing to delete"<>n; + bst.del(n); + break; + case 3: + cout<<"Search:"<>item; + bst.search(r,item); + break; + case 4: + cout<<"Inorder Traversal of BST:"< +using namespace std; +void swapping(int &a, int &b) { //swap the content of a and b + int temp; + temp = a; + a = b; + b = temp; +} +void display(int *array, int size) { + for(int i = 0; i array[j+1]) { //when the current item is bigger than next + swapping(array[j], array[j+1]); + swaps = 1; //set swap flag + } + } + if(!swaps) + break; // No swap in this pass, so array is sorted + } +} +int main() { + int n; + cout << "Enter the number of elements: "; + cin >> n; + int arr[n]; //create an array with given number of elements + cout << "Enter elements:" << endl; + for(int i = 0; i> arr[i]; + } + cout << "Array before Sorting: "; + display(arr, n); + bubbleSort(arr, n); + cout << "Array after Sorting: "; + display(arr, n); +} diff --git a/Factorial b/Factorial new file mode 100644 index 000000000..d18c0983a --- /dev/null +++ b/Factorial @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main() +{ + unsigned int n; + unsigned long long factorial = 1; + + cout << "Enter a positive integer: "; + cin >> n; + + for(int i = 1; i <=n; ++i) + { + factorial *= i; + } + + cout << "Factorial of " << n << " = " << factorial; + return 0; +} diff --git a/Matrix multiplication b/Matrix multiplication new file mode 100644 index 000000000..1f2fd1f90 --- /dev/null +++ b/Matrix multiplication @@ -0,0 +1,70 @@ +#include +using namespace std; + +int main() +{ + int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; + + cout << "Enter rows and columns for first matrix: "; + cin >> r1 >> c1; + cout << "Enter rows and columns for second matrix: "; + cin >> r2 >> c2; + + // If column of first matrix in not equal to row of second matrix, + // ask the user to enter the size of matrix again. + while (c1!=r2) + { + cout << "Error! column of first matrix not equal to row of second."; + + cout << "Enter rows and columns for first matrix: "; + cin >> r1 >> c1; + + cout << "Enter rows and columns for second matrix: "; + cin >> r2 >> c2; + } + + // Storing elements of first matrix. + cout << endl << "Enter elements of matrix 1:" << endl; + for(i = 0; i < r1; ++i) + for(j = 0; j < c1; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> a[i][j]; + } + + // Storing elements of second matrix. + cout << endl << "Enter elements of matrix 2:" << endl; + for(i = 0; i < r2; ++i) + for(j = 0; j < c2; ++j) + { + cout << "Enter element b" << i + 1 << j + 1 << " : "; + cin >> b[i][j]; + } + + // Initializing elements of matrix mult to 0. + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + { + mult[i][j]=0; + } + + // Multiplying matrix a and b and storing in array mult. + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + for(k = 0; k < c1; ++k) + { + mult[i][j] += a[i][k] * b[k][j]; + } + + // Displaying the multiplication of two matrix. + cout << endl << "Output Matrix: " << endl; + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + { + cout << " " << mult[i][j]; + if(j == c2-1) + cout << endl; + } + + return 0; +} diff --git a/Palindrome b/Palindrome new file mode 100644 index 000000000..64841a3f7 --- /dev/null +++ b/Palindrome @@ -0,0 +1,28 @@ +#include +using namespace std; + +int main() +{ + int n, num, digit, rev = 0; + + cout << "Enter a positive number: "; + cin >> num; + + n = num; + + do + { + digit = num % 10; + rev = (rev * 10) + digit; + num = num / 10; + } while (num != 0); + + cout << " The reverse of the number is: " << rev << endl; + + if (n == rev) + cout << " The number is a palindrome."; + else + cout << " The number is not a palindrome."; + + return 0; +}