473,238 Members | 1,593 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,238 software developers and data experts.

Greatest common divisor

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
  2. #include <stdio.h>
  4. int gcd(int a, int b);
  6. int main(void)
  7. {
  8.     int a, b,r;
  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. }
  17. int gcd(int a, int b)
  18. {
  19.     int r;
  21.     if ((r = a % b) == 0)
  22.         return b;
  23.     else
  24.         return gcd(b, r);
  25. }
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 3441
never mind figured it out.
Sep 16 '08 #2
2,426 Expert 2GB
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.
Sep 17 '08 #3
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
9,065 Expert Mod 8TB
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.

Sep 18 '08 #5

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

Similar topics

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...
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...
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...
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.*;...
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...
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...
by: 7asco | last post by:
i want the code of greater common divisor algorithm in c++
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++ //...
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) )...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.