#include<iostream> usingnamespace std ; int main() { Declare x, y, temp, remaind
ID: 3610021 • Letter: #
Question
#include<iostream>
usingnamespace std ;
int main()
{
Declare x, y, temp, remainder asInteger.
// read in the two integers
cout << endl ;
cout << "Enter the first number (positiveinteger) : " ;
cin >> x ;
cout << "Enter the second number (positiveinteger) : " ;
cin >> y ;
//echo inputs
cout << "Input numbers are: " << x<< " , " << y << endl ;
Writea C++ if statement to determine if x < y.
{ // exchange values of x andy
Write three assignment statements in C++that are
equivalent to thethree statements given in the
pseudocode.
}
/* At this point we will always have x >=y */
Initialize remainder.
while ( )
{
Write the loop expressionand loop body code in C++.
In C++, the expression (x % y) gives the remainder
after dividing x byy.
}
// display the result
cout << endl ;
cout << "The GCD is: " << y<< endl ;
return (0); // terminate withsuccess
}
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int x=0,y=0,remainder=4,temp=0;
cout << endl;
cout << "Enter the first number (positiveinteger) : " ;
cin >> x ;
cout << "Enter the second number (positiveinteger) : " ;
cin >> y ;
//echoinputs
cout<< "Input numbers are: " << x << " , " << y<< endl ;
if(x>y)
{
temp=x;
x=y;
y=temp;
}
while(remainder!=1 && remainder!=0)
{
remainder=y%x;
y=x;
x=remainder;
}
if(remainder==1) // if remainder is 1 then thayhave no common divisor
y=remainder;
cout<<"gcd = "<<y<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.