Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links


Program to print GCD of given numbers

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 27-11-2009
Member
 
Join Date: Nov 2009
Posts: 4
Program to print GCD of given numbers

Sponsored Links
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.
Reply With Quote
  #2  
Old 27-11-2009
Modifier's Avatar
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<conio.h>
#include<stdio.h>

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;
}
Reply With Quote
  #3  
Old 27-11-2009
Praetor's Avatar
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..
Reply With Quote
  #4  
Old 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 <iostream>

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 << " , " <<q<< endl ;


if ((p == 0) || (q == 0))
{
cout << "ERROR: Value of the 0 has been entered." ;
return (-1);
}

if (p < q)
{

t = p;
p = q;
q = t;

}


while ( r != 0 )

{

t = p / q;

r = x % y;

p = q;

p = r;

}


cout << endl ;
cout << "The GCD of given numbers is: " << q << endl ;

return (0);
}
Reply With Quote
  #5  
Old 27-11-2009
kelfro's Avatar
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;
}
Reply With Quote
  #6  
Old 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;
}
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Program to print GCD of given numbers"
Thread Thread Starter Forum Replies Last Post
C Program to print Combination of numbers Prashobh Mallu Software Development 6 26-03-2010 09:21 PM
Need C program code to count numbers of negative & positive numbers Sarfaraj Khan Software Development 5 16-01-2010 01:00 PM
C sharp program to print random numbers Prashobh Mallu Software Development 5 12-01-2010 11:07 AM
Program to print odd and even numbers roodiii Software Development 3 30-11-2009 01:13 PM
What is the program to swap two numbers? Bharat89 Software Development 3 26-11-2009 08:37 AM


All times are GMT +5.5. The time now is 04:35 PM.