Question: The gcd program of Example 1.20 might also be written int main() { int i = getint(), j = getint(); while (i != j) {

The gcd program of Example 1.20 might also be written

int main() {
    int i = getint(), j = getint();
    while (i != j) {
        if (i > j) i = i % j;
        else j = j % i;
}
putint(i);

}

Does this program compute the same result? If not, can you fix it? Under what circumstances would you expect one or the other to be faster?

Step by Step Solution

3.44 Rating (163 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The difference between the two programs is in the two assignment statements i i j and j j i versus i ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!