TechArena Community Program to print GCD of given numbers

# Program to print GCD of given numbers

## Software Development

#1
27-11-2009
 Member Join Date: Nov 2009 Posts: 4
Program to print GCD of given numbers

Hi All,

I need you help in writing a program in C or C++ programming language.
Suppose we have a program while execution of the program code, if we pass the two integer number then the desired output should be the GCD(Greatest Common Divisor) of the given two numbers.

Please provide the program code to print the GCD of the given number. Even hint also will works for me.

Your help will be greatly appreciated.

Last edited by Bhavesh_seth : 27-11-2009 at 07:56 AM.
#2
27-11-2009
 Member Join Date: Jan 2008 Posts: 1,507
Re: Program to print GCD of given numbers

Hi friends,

I have used the the concept of the "Goto" statement to find out GCD of the two given number.
I think it will also easy for you to understand the "Goto" statement than complex "For" loop. Please study below program carefully:

Quote:
 #include #include int main( ) { int x, y, tm, rm, clrscr( ); printf("Enter the two number:"); scanf(" %d%d",&x, &y); if(x==0) { printf("\n GCD of number is : %d", y); goto last; } if(b==0) { printf("\n GCD of number is : %d", x); goto last; } /*I took a empty 'for' loop for( ; ; ) { rm=x%y; if(rm==0) break; x= y; x= rm; } printf("\n GCD of Number is : %d", y); last: getch( ); return 0; }
#3
27-11-2009
 Member Join Date: Apr 2008 Posts: 1,943
Re: Program to print GCD of given numbers

Hi,

You should use "While loop" to find out the GCD of the inputted numbers. For reference take the following example which will calculates the GCD of numbers "a1" and "a2". The condition "a1!=a2" from the "While loop" first check whether two variables are identical or not:

Quote:
 void main() { int a1,a2; clrscr(); printf("\nEnter two numbers:"); scanf("%d %d",&a1,&a2); while(a1!=a2) { if(a1>=a2-1) a1=a1-a2; else a2=a2-a1; } printf("GCD of given numbers =%d",a1); getch(); }
I hope above program may helpful for you..
#4
27-11-2009
 Member Join Date: May 2008 Posts: 2,000
Re: Program to print GCD of given numbers

Dear friend,

I get below program from one of the C programming book, and which will explain you the logic of finding GCD of the given number:

Quote:
 #include int main() { int p; int Q; int t ; int r = 1; cout << endl ; cout << "Enter the first number (positive integer) : " ; cin >> p ; cout << "Enter the second number (positive integer) : " ; cin >> q; cout << "Inputted numbers are: " << p << " , " <
#5
27-11-2009
 Member Join Date: Apr 2008 Posts: 1,989
Re: Program to print GCD of given numbers

It is very easy for me to write the program for GCD of 2 numbers. As I have sound knowledge about the various looping. Take below code and try to run. This code may contains some syntax error as I haven't executed it.

Quote:
 void main() { int p1,p2,gcd_demo; clrscr(); printf("Enter numbers: "); scanf("%d %d",&p1,&p2); gcd_demo=findgcd_demo(p1,p2); printf("\nGCD of %d and %d is: %d",p1,p2,gcd); getch(); } int findgcd_demo(int l,int m) { while(l!=m) { if(l>m) return findgcd_demo(l-m,m); else return findgcd_demo(l,m-l); } return l; }
#6
15-03-2011
 Member Join Date: Mar 2011 Posts: 1
Re: Program to print GCD of given numbers

#include<stdio.h>
main()
{
int x,y,i;
printf("\n enter two numbers:");
scanf("%d%d",&x,&y);
for(i==x;i>=1;i--)
{
if(x%i==0&&y%%i==0)
{
printf("\n gcd of two numbers is %d",i);
break;
}
}
}