Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Take the program below and use a for loop instead of a while loop to calculate t

ID: 3536376 • Letter: T

Question

Take the program below and use a for loop instead of a while loop to calculate the gcd.

//Calculate the Greatest Common Divisor


#include <iostream>

using namespace std;


int main()

{

int x; //first number

int y; //second number

int remainder = 0;


cout << "Enter two integers greater than zero: ";

cin >> x >> y;


cout << "The Greatest Common Divisor of " << x << " and " << y << " is: ";

//Calculate the GCD using Euclidean's algorithm

int GCD ( int x, int y)

{

int i;

if (x<y)

{

for (i=x;i>=0;i--)

{

if(x%i==0 &&y%1==0)

{return i;}

}

}

else

{

for (i=y;i>0;i--)

{if (x%i==0 && y%i==0)

{return i;}

}

}

return i;

}//end main

return i;

}

}

Explanation / Answer


#include <iostream>

using namespace std;

int GCD(int,int);


int main()

{

int x; //first number

int y; //second number

int remainder = 0;


cout << "Enter two integers greater than zero: ";

cin >> x >> y;


cout << "The Greatest Common Divisor of " << x << " and " << y << " is: ";

  

cout<<GCD(x,y);



return 0;

}//end main



//Calculate the GCD using Euclidean's algorithm


int GCD ( int x, int y)

{

int i;

if (x<y)

{

i=x;

while(i>0)

{

if(y%i==0 && x%i==0)

{

return i;

}


i--;

}

}

else

{

i=y;

while(i>0)

{

if(y%i==0 && x%i==0)

{

return i;

}


i--;

}

}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote