Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- using namespace std;
- bool isRelativePrime(int a, int b) {
- if (a == b)
- return false;
- else if (a > b)
- swap(a, b);
- if (b % a == 0)
- return false;
- for (int i = 2; i * i <= a; i++)
- if (!(b % i) && !(a % i))
- return false;
- return true;
- }
- int main() {
- cout << "Size of the (int) Array: ";
- int n;
- cin >> n;
- int a[n];
- bool arraySet = false;
- while (true) {
- cout << endl << endl;
- cout << "1. Enter the Array Elements" << endl;
- cout << "2. Print the Array in Reverse Order" << endl;
- cout << "3. Find the Number of Odd and Even Numbers in the Array" << endl;
- cout << "4. Find the Maximum and Minimum Values in the Array" << endl;
- cout << "5. Find the Number of Relative Primes in the Array" << endl;
- cout << "0. Exit" << endl;
- cout << endl;
- cout << "Enter Choice: ";
- int choice;
- cin >> choice;
- if (choice == 1) {
- cout << "Enter " << n << " Elemnts: ";
- for (int i = 0; i < n; i++)
- cin >> a[i];
- arraySet = true;
- }
- else if (choice == 2) {
- if (!arraySet)
- cout << "Array Elements Not Entered. Please Enter the Array Elements First" << endl;
- else {
- cout << "Printing Array ELements in Reverse: ";
- for (int i = n - 1; i >= 0; i--)
- cout << a[i] << " ";
- cout << endl << endl;
- }
- }
- else if (choice == 3) {
- if (!arraySet)
- cout << "Array Elements Not Entered. Please Enter the Array Elements First" << endl;
- else {
- int odd = 0, even = 0;
- for (int i = 0; i < n; i++) {
- if (a[i] % 2) odd++;
- else even++;
- }
- cout << "ODD Numbers: " << odd << endl;
- cout << "EVEN Numbers: " << even << endl;
- cout << endl << endl;
- }
- }
- else if (choice == 4) {
- if (!arraySet)
- cout << "Array Elements Not Entered. Please Enter the Array Elements First" << endl;
- else {
- int max = a[0];
- int min = a[0];
- for (int i = 1; i < n; i++) {
- if (a[i] < min) min = a[i];
- else if (a[i] > max) max = a[i];
- }
- cout << "Maximum Value: " << max << endl;
- cout << "Minimum Value: " << min << endl;
- cout << endl << endl;
- }
- }
- else if (choice == 5) {
- if (!arraySet)
- cout << "Array Elements Not Entered. Please Enter the Array Elements First" << endl;
- else {
- int count = 0;
- for (int i = 0; i < n - 1; i++)
- for (int j = i + 1; j < n; j++)
- if (isRelativePrime(a[i], a[j])) count++;
- cout << "Number of Relative Primes: " << count << endl;
- cout << endl << endl;
- }
- }
- else if (choice == 0)
- break;
- else {
- cout << "Invalid Choice!!!" << endl;
- cout << "Try Again." << endl;
- continue;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement