473,472 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Greatest common divisor

19 New Member
Hi I'm trying to write a program to find the greatest common divisor of 2 numbers input by the user. This is what I got so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. int gcd(int a, int b);
  5.  
  6. int main(void)
  7. {
  8.     int a, b,r;
  9.  
  10.     printf("Please input two positive integers");
  11.     scanf("%d%d", &a, &b);
  12.     gcd(a, b);
  13.     if (gcd(b, r))
  14.         printf("%d", gcd);
  15. }
  16.  
  17. int gcd(int a, int b)
  18. {
  19.     int r;
  20.  
  21.     if ((r = a % b) == 0)
  22.         return b;
  23.     else
  24.         return gcd(b, r);
  25. }
  26.  
  27.  
but being the newb that I am. I can't figure out a way to take the data from my gcd function and print it on screen. Any help on ANYTHING I am doing wrong is much appreciated.
Sep 16 '08 #1
4 3452
jmf777
19 New Member
never mind figured it out.
Sep 16 '08 #2
donbock
2,426 Recognized Expert Top Contributor
Notice that your gcd function assumes that 'a' is always bigger than 'b'. You might want to add some code to your main function to insure that the first call to gcd() honors that assumption. It would also be a good idea to add comments to gcd stating this assumption.
Cheers,
donbock
Sep 17 '08 #3
shakeel225
4 New Member
hello
i am a cse student (2nd year) in nit srinagar doing B.Tech.
u can try the follwing code.

<full code solution snipped>
Sep 18 '08 #4
Banfa
9,065 Recognized Expert Moderator Expert
Hi shakeel225,

posting full code solutions in not allowed on this forum particularly to fairly simple problems that may well be course work or a class assignment.

Please read our posting guidelines where all this is explained.

Banfa
Administrator.
Sep 18 '08 #5

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

Similar topics

8
by: Mike Nolan | last post by:
As far as I can tell, Postgres has no equivalent to greatest and least functions in Oracle. Yes, you can do the same thing with a case statement, but at the expense of writing MUCH longer SQL...
7
by: Mathew Butler | last post by:
Suppose I have a table t with columns id, col1, col2, col3, col4, col5, col6 all numeric. I want to query the table and for each value of col<x> in the resultset I want to identify the largest value...
21
by: Frederick Gotham | last post by:
I'm trying to devise a compile-time constant for X, where X is the greatest number which satisfies both the following criteria: (1) X <= DESIGNATED_MAX_VALUE (2) X % Y == 0 I'll try to...
7
by: cess | last post by:
Hi!!! i would like to know if what is lacking in the codes below to have a greatest common denominator of two given(by the user) numbers?? I'm confused and i need your help! import java.io.*;...
4
by: sdlt85 | last post by:
Hi, Can someone help me with an idea on how to start writing a C++ code for generating greatest common divisor and the linear combination of two intergers represented as gcd(m, n)= mx + ny and...
15
by: judge82 | last post by:
I'm new to Java and I was looking for some help on this particular problem. I need to create a method that can simplify a fraction, that is, represent it as a fraction where the numerator and the...
2
by: 7asco | last post by:
i want the code of greater common divisor algorithm in c++
3
by: stressedstudent | last post by:
I dont know where I am going wrong so I dont know which part to post, this is what I have, can anyone help me figure out where I am going wrong? THanks for any and all help. // into to c++ //...
35
by: aarklon | last post by:
Hi all, The following question is asked frequently in interviews How to find the greatest of 2 numbers without using relational operators ? the solution i have seen is ( a+b + abs(a-b) )...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.