473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Please Help me with this one problem.

10
The greatest common divisor of two positive integers is the largest integer that is a divisor of both of them. For example, 6 and 15 have 3 as their greatest common divisor, and 15 and 22 have 1 as their greatest common divisor. The following recursive function computes the greatest common divisor of two positive integers. First write a program to test the function; then write and test an equivalent iterative function.

int gcd(int a, int b)
{
int r;

if ((r = a % b) = = 0)
return b;
else
return gcd(b, r);
}
Dec 7 '06 #1
3 1346
Manjiri
40
The greatest common divisor of two positive integers is the largest integer that is a divisor of both of them. For example, 6 and 15 have 3 as their greatest common divisor, and 15 and 22 have 1 as their greatest common divisor. The following recursive function computes the greatest common divisor of two positive integers. First write a program to test the function; then write and test an equivalent iterative function.

int gcd(int a, int b)
{
int r;

if ((r = a % b) = = 0)
return b;
else
return gcd(b, r);
}


Here i have the solution..
Is it required one...?
Plz reply back..


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. int a,b,n,gcdnum=0,i;
  6. printf("enter two numbers\n");
  7. scanf("%d%d",&a,&b);
  8. n=(a<b)?a:b;
  9. for(i=1; i<=a; i++)
  10. {
  11. if(a%i==0 && b%i==0)
  12. {
  13. if(gcdnum<i)
  14. gcdnum=i;
  15. else
  16. gcdnum=gcdnum;
  17. }
  18. }
  19. printf("Greatest common devisor is %d",gcdnum);
  20. return 0;
  21. }
Dec 7 '06 #2
DeMan
1,806 1GB
I don't mean to be horribly pedantic, but the else statement is unnecessary. gcdnum will stay gcdnum if we don't do anything, so include the {if then change} but you can safely omit the else
Dec 7 '06 #3
alisbub
10
Here i have the solution..
Is it required one...?
Plz reply back..


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. int a,b,n,gcdnum=0,i;
  6. printf("enter two numbers\n");
  7. scanf("%d%d",&a,&b);
  8. n=(a<b)?a:b;
  9. for(i=1; i<=a; i++)
  10. {
  11. if(a%i==0 && b%i==0)
  12. {
  13. if(gcdnum<i)
  14. gcdnum=i;
  15. else
  16. gcdnum=gcdnum;
  17. }
  18. }
  19. printf("Greatest common devisor is %d",gcdnum);
  20. return 0;
  21. }
That worked thanks for your help.
Dec 7 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Duane Lambe | last post by:
I'll start out by saying that I've been looking for a definitive answer for a few hours now, and need opinions on this one. I have a site I've been starting for our internal helpdesk joint....
0
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
2
by: Carlo, MCP | last post by:
Hi, Sorry for posting twice, but I hope in your comprehension. Please help me! I'm troubling from months with a serious serialization problem that I'm not able to solve. I try to describe as...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
1
by: funfair | last post by:
HI,EVERY ONE first problem, i have create a database in access 2003 it worked fine untill i have format my laptop . now im working on office 2003 on windows xp and i have norton 2006 but im...
1
by: oldgent | last post by:
I am having a problem installing the starter kits. I have reinstalled VS 2005, think that might be the problem. I then installed both 'Personal Website" and the "Club Website" starter kits. I...
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
1
by: icetalks | last post by:
have a look at this code , its for logging the user in after checking his UserName and Password. dim check as boolean = false ... ... If txtUserName.Text.Length = 0 And txtPass.Text.Length =...
97
by: luap | last post by:
Hello good friends, please i need your help and advice on how i should best programme the problem, if possible a different option to solve it is welcomed. The problem a matching problem, how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.