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: -
-
#include <stdio.h>
-
-
int gcd(int a, int b);
-
-
int main(void)
-
{
-
int a, b,r;
-
-
printf("Please input two positive integers");
-
scanf("%d%d", &a, &b);
-
gcd(a, b);
-
if (gcd(b, r))
-
printf("%d", gcd);
-
}
-
-
int gcd(int a, int b)
-
{
-
int r;
-
-
if ((r = a % b) == 0)
-
return b;
-
else
-
return gcd(b, r);
-
}
-
-
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.
4 3475
never mind figured it out.
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
hello
i am a cse student (2nd year) in nit srinagar doing B.Tech.
u can try the follwing code.
<full code solution snipped>
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.
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 statements.
Is this something that is on or can be added to the 'to do' list?
I could write a series of user-defined functions to do specific
comparisons (such as comparing several dates and returning the greatest
one) but there doesn't appear to...
|
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 in each of the columns. For example, with the following dataset:
id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1
I want a query to generate the following output:
1,7
|
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 explain with example code:
|
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.*;
public class gcd {
public static void main (String args)throws Exception
{
String number;
|
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 adding them it will give us the
greatest common divisor and I need to use the extended Euclidean
algorithm.
I already have the code for the greatest common divisor but I dont
know how to do the linear combination.
This is what I have:
| |
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 denominator have no common divisors. it should read in the numerator and the denominator and then print out the fraction in its simplified form.
I tried to calculate it with:
while n!=0
{
r = m mod n;
m = n;
n = r;
|
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++
// This program should use a loop that lets the user enter a series of integers.
// The user should enter -99 to signal the end of the series.
// After all the numbers have been entered,
// the program should display the largest and smallest numbers...
|
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) ) /2 ;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |