Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- int arr[10] = {0}, n, LG, LCM, GCD, i, j;
- while(true) {
- cout << "OPERATIONS" << endl;
- cout << "1. LCM (Lowest Common Multiple)" << endl;
- cout << "2. GCD (Greatest Common Divisor)" << endl;
- cout << "0. Exit." << endl;
- cout << "Enter Choice: ";
- cin >> LG;
- if(LG == 1 || LG == 2) {
- while(true) {
- cout << "How Many Elements? (Maximum 10) : ";
- cin >> n;
- if(n > 10 || n < 1)
- cout << "Invalid Input. Try Again." << endl;
- else
- break;
- }
- cout << "Enter the Elements (Only Non-Zero Positive Integers): ";
- for(i = 0; i < n; i++)
- cin >> arr[i];
- LCM = 9999, GCD = -1;
- for(i = 0; i < n; i++) {
- while(true) {
- if(arr[i] < 1) {
- cout << "Element no. " << i + 1 << " is invalid. Renter The Element: ";
- cin >> arr[i];
- }
- else {
- if(arr[i] < LCM) LCM = arr[i];
- if(arr[i] > GCD) GCD = arr[i];
- break;
- }
- }
- }
- if(LG == 1) {
- for(i = LCM; ; i++) {
- for(j = 0; j < n; j++)
- if(i % arr[j] != 0) break;
- if(j == n) {
- LCM = i;
- break;
- }
- }
- cout << "LCM = " << LCM << endl << endl << endl;
- }
- else {
- for(i = GCD; i > 0; i--) {
- for(j = 0; j < n; j++)
- if(arr[j] % i != 0) break;
- if(j == n) {
- GCD = i;
- break;
- }
- }
- cout << "GCD = " << GCD << endl << endl << endl;
- }
- }
- else if(LG == 0) break;
- else cout << "Invalid Choice. Try Again." << endl << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement