answersLogoWhite

0


Best Answer

One way to find the GCD (Greatest Common Divisor) of two numbers is Euclid's method. The following program demostrates this, without using recursion. The third number printed is the GCD of the first two. The highlighted lines are the core of the algorithm.

#include

int GcdByEuclid (int a, int b) {

if (a < 0 b < 0) return -1;

while (a > 0 && b > 0) if (a > b) a -= b; else b -= a;

if (a == 0) return b; else return a;

}

int main (int argc, char *argv[]) {

int a, b;

if (argc < 3) {

fprintf (stderr, "Usage: gcd a b\n");

return 1;

}

a = atoi(argv[1]);

b = atoi(argv[2]);

printf ("%d %d %d", a, b, GcdByEuclid (a, b));

return 0;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a C program to find GCD of 2 numbers using non-recursion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How to write a C program to find largest 2 numbers using pointers?

program to find maximum of two numbers using pointers


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


Write a program to find the product of two numbers using Halving and Doubling method?

i need this answer


Write a program large number and small number among n numbers by using If statement?

12


Write a program to swap two numbers using function?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }


Write a C program to find the square of two numbers?

Please visit http://talentsealed.blogspot.com/2009/10/to-find-sqaure-of-numbers-using-c.htmlfor the answer.


Write a sample program using ASPNET explaining all the syntax and semantics of the program?

write a sample program using asp.net explaining all the syntax and semantics of the program


Write a program in C language to multiply any two numbers without using in source code?

The question is malformed and incomprehensible.


How do you write Square program using vb?

write a vb program to find the magic square


Write Client and server program in C language using UDP?

Write and run a client and a server program in C-language using UDP


Program to count the number of numbers in an array using 8085 microprocessor?

A program which is used to count the number of numbers in an array using a 8085 microprocessor is known as a assembly language program.