+ 1

Two pairs and two GCD

What is wrong with this code? #include<iostream> using namespace std; int main() { int first_number; cout<<"Enter First Number : "; cin>>first_number; int second_number; cout<<"Enter Second Number: "; cin>>second_number; int third_number; cout<<"Enter Third Number : "; cin>>third_number; int fourth_number; cout<<"Enter Fourth Number: "; cin>>fourth_number; int gcd1; for(int i=1;i<=first_number&&i<=second_number;i++){ if(first_number%i==0 && second_number%i == 0 ){ gcd1=i; } int gcd2; for(int i=1;i<=third_number&&i<=fourth_number;i++){ if(third_number%i==0 && fourth_number%i == 0 ){ gcd2=i; } } cout<<"Greatest Common Divison (GCD1):"<<gcd1<<endl; cout<<"Greatest Common Divison (GCD2):"<<gcd2<<endl; return 0; }} Why answer is 1 and 15, if first number is 9, second number is 27, third number is 15 and fourth number is 45? I suppose, there is very simple mistake.

31st Oct 2016, 6:55 PM
Raimonds MeikĆĄa
Raimonds MeikĆĄa - avatar
1 Answer
0
Code looks good, but you have 2 closing brackets after return 0. Move one of them after first for loop, because there is missing closing bracket.
31st Oct 2016, 7:46 PM
Daniel Oravec
Daniel Oravec - avatar