Question

In: Computer Science

In C language Please display results The Euclidean algorithm is a way to find the greatest...

In C language

Please display results

The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45.

Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30.

Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15.

Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0.

The greatest common divisor of 210 and 45 is 15.

Formal description of the Euclidean algorithm

Input Two positive integers, a and b.

Output The greatest common divisor, g, of a and b.

Internal computation

1. If a<b, exchange a and b.

2. Divide a by b and get the remainder, r. If r=0, report b as the GCD of a and b.

3. Replace a by b and replace b by r. Return to the previous step.

Your assignment is to write an assembler code (gcd.s) that asks the user two positive numbers and calculates their greatest common denominator.

For example, you program should produce the following outputs:

Enter first positive integer: 6

Enter second positive integer: 8

The GCD is 2

Solutions

Expert Solution

// do comment if any problem arises

//code

#include <stdio.h>

//this function swaps given integers

void swap(int*a,int*b)

{

    int temp=*a;

    *a=*b;

    *b=temp;

}

//this function returns gcd of a and b

int gcd(int a,int b)

{

    if(a<b)

    swap(&a,&b);

    int r=a%b;

    if(r==0)

    return b;

    return gcd(b,r);

}

int main()

{

    int first,second;

    printf("Enter first positive integer: ");

    scanf("%d",&first);

    printf("Enter second positive integer: ");

    scanf("%d",&second);

    printf("The GCD is %d",gcd(first,second));

}

Output:

  


Related Solutions

4. (a) Use the Euclidean algorithm to find the greatest common divisor of 21 and 13,...
4. (a) Use the Euclidean algorithm to find the greatest common divisor of 21 and 13, and the greatest common divisor of 34 and 21. (b) It turns out that 21 and 13 is the smallest pair of numbers for which the Euclidean algorithm requires 6 steps (for every other pair a and b requiring 6 or more steps a > 21 and b > 13). Given this, what can you say about 34 and 21? (c) Can you guess...
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of...
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of a mod n. Your program should allow user to enter a and n. Note: For this question please make sure the code compiles and runs, it is not copied and pasted from elsewhere( I will be checking!). Thanks
All necessary steps much show for these problems, please. Use the Euclidean algorithm to find gcd(12345,...
All necessary steps much show for these problems, please. Use the Euclidean algorithm to find gcd(12345, 54321). Write gcd(2420, 70) as a linear combination of 2420 and 70. The work to obtain the gcd is provided. 2420 = 34(70) + 40 70 = 1(40) + 30 40 = 1(30) + 10 30 = 3(10) + 0 Determine if 1177 is prime or not. If it is not, then write 1177 as a product of primes Find gcd(8370, 465) by unique...
Use the Euclidean algorithm to find the GCD of 3 + 9i and 7-i
Use the Euclidean algorithm to find the GCD of 3 + 9i and 7-i
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
please i want solution for this question with algorithm and step by step with c++ language...
please i want solution for this question with algorithm and step by step with c++ language write a program using for loop that calculates the total grade for N classroom exercises as a percentage. The user should input the value for N followed by each of the N scores and totals. Calculate the overall percentage (sum of the total points earned divided by the total points possible) and output it as a percentage. Sample input and output is shown below.How...
use euclidean algorithm to find integers m,n such that 1693m+2019n=1
use euclidean algorithm to find integers m,n such that 1693m+2019n=1
Using Euclidean algorithm, Find integers x and y with 65537x + 3511y = 17.
Using Euclidean algorithm, Find integers x and y with 65537x + 3511y = 17.
find a linear combination for gcd(259,313). use extended euclidean algorithm. what is inverse of 259 in...
find a linear combination for gcd(259,313). use extended euclidean algorithm. what is inverse of 259 in z subscript 313? what is inverse of 313 in z subscript 259?
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT