Advertisement
skb50bd

207Lec1HW

May 16th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. bool isRelativePrime(int a, int b) {
  7.     if (a == b)
  8.         return false;
  9.     else if (a > b)
  10.         swap(a, b);
  11.  
  12.     if (b % a == 0)
  13.         return false;
  14.  
  15.     for (int i = 2; i * i <= a; i++)
  16.         if (!(b % i) && !(a % i))
  17.             return false;
  18.  
  19.     return true;
  20. }
  21.  
  22.  
  23. int main() {
  24.     cout << "Size of the (int) Array: ";
  25.     int n;
  26.     cin >> n;
  27.     int a[n];
  28.     bool arraySet = false;
  29.  
  30.     while (true) {
  31.         cout << endl << endl;
  32.         cout << "1. Enter the Array Elements" << endl;
  33.         cout << "2. Print the Array in Reverse Order" << endl;
  34.         cout << "3. Find the Number of Odd and Even Numbers in the Array" << endl;
  35.         cout << "4. Find the Maximum and Minimum Values in the Array" << endl;
  36.         cout << "5. Find the Number of Relative Primes in the Array" << endl;
  37.         cout << "0. Exit" << endl;
  38.         cout << endl;
  39.         cout << "Enter Choice: ";
  40.         int choice;
  41.         cin >> choice;
  42.  
  43.         if (choice == 1) {
  44.             cout << "Enter " << n << " Elemnts: ";
  45.             for (int i = 0; i < n; i++)
  46.                 cin >> a[i];
  47.             arraySet = true;
  48.         }
  49.  
  50.         else if (choice == 2) {
  51.             if (!arraySet)
  52.                 cout << "Array Elements Not Entered. Please Enter the Array Elements First" << endl;
  53.             else {
  54.                 cout << "Printing Array ELements in Reverse: ";
  55.                 for (int i = n - 1; i >= 0; i--)
  56.                     cout << a[i] << " ";
  57.                 cout << endl << endl;
  58.             }
  59.         }
  60.  
  61.         else if (choice == 3) {
  62.             if (!arraySet)
  63.                 cout << "Array Elements Not Entered. Please Enter the Array Elements First" << endl;
  64.             else {
  65.                 int odd = 0, even = 0;
  66.                 for (int i = 0; i < n; i++) {
  67.                     if (a[i] % 2) odd++;
  68.                     else even++;
  69.                 }
  70.                 cout << "ODD Numbers: " << odd << endl;
  71.                 cout << "EVEN Numbers: " << even << endl;
  72.                 cout << endl << endl;
  73.             }
  74.         }
  75.  
  76.         else if (choice == 4) {
  77.             if (!arraySet)
  78.                 cout << "Array Elements Not Entered. Please Enter the Array Elements First" << endl;
  79.             else {
  80.                 int max = a[0];
  81.                 int min = a[0];
  82.                 for (int i = 1; i < n; i++)  {
  83.                     if (a[i] < min) min = a[i];
  84.                     else if (a[i] > max) max = a[i];
  85.                 }
  86.                 cout << "Maximum Value: " << max << endl;
  87.                 cout << "Minimum Value: " << min << endl;
  88.                 cout << endl << endl;
  89.             }
  90.         }
  91.  
  92.         else if (choice == 5) {
  93.             if (!arraySet)
  94.                 cout << "Array Elements Not Entered. Please Enter the Array Elements First" << endl;
  95.             else {
  96.                 int count = 0;
  97.                 for (int i = 0; i < n - 1; i++)
  98.                     for (int j = i + 1; j < n; j++)
  99.                         if (isRelativePrime(a[i], a[j])) count++;
  100.                 cout << "Number of Relative Primes: " << count << endl;
  101.                 cout << endl << endl;
  102.             }
  103.         }
  104.  
  105.         else if (choice == 0)
  106.             break;
  107.  
  108.         else {
  109.             cout << "Invalid Choice!!!" << endl;
  110.             cout << "Try Again." << endl;
  111.             continue;
  112.         }
  113.  
  114.     }
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement