473,386 Members | 1,694 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.

Why does the below code not produce any output ?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int gcd(int m ,int n)
  3. {
  4. if(m%n==0)
  5. return n;
  6. else return(n,m%n);
  7. }
  8.  
  9. int main()
  10. {
  11. int x,y;
  12. printf("enter two numbers");
  13. scanf("%d",&x);
  14. scanf("%d",&y);
  15. int a=gcd(x,y);
  16. if(a==1)
  17. {
  18. printf("the numbers are co-prime");
  19. }
  20. return 0;
  21. }

Here when I run the code just after taking input there is no output so what is the reason for it , I gave input as 7 and 9 and I didn't get any output ,plz clarify this .
Aug 10 '15 #1
3 1334
weaknessforcats
9,208 Expert Mod 8TB
What is gcd returning? If m is 7 and n is 9 then m%n is 7%9 which is 7. So then m%n ==0 is false. Therefore you return (n,m%n). This looks odd but it is a valid a valid expression using the comma operator. SO first there's n and then there's m%n and that's what is returned.

Two things, first write your code such that you can see the return value from your functions. Second, I have the feeling this gcd function is recursive but you have omitted the gcd in that return statement.

Post again and let me know what happened.
Aug 10 '15 #2
First it would be
gcd(7,9) --> gcd(9,7)--> gcd(7,2) -->gcd(2,1) now since m%n==0 hence it should return n which is equal to 1 , where's the problem then ?why is the code not producing the output ?
Aug 11 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
If you look inside the gcd function you see:

Expand|Select|Wrap|Line Numbers
  1. int gcd(int m ,int n)
  2.  {
  3.  if(m%n==0)
  4.  return n;
  5.  else return(n,m%n);   <----------------
  6.  }
  7.  
So it's not doing this: gcd(7,9) --> gcd(9,7)--> gcd(7,2) -->gcd(2,1)

I think the code should be:

Expand|Select|Wrap|Line Numbers
  1. int gcd(int m ,int n)
  2.  {
  3.  if(m%n==0)
  4.  return n;
  5.  else return gcd(n,m%n);   <----------------revised
  6.  }
  7.  
This is a case where stepping though the code with a debugger can save a lot of time.
Aug 11 '15 #4

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

Similar topics

16
by: Daniel | last post by:
Hi Group, Why does the below code not generate an error? I have a table with x number of records. Each record is read from the DB into a hashtable. Then I add to the hashtable additional keys....
0
by: J | last post by:
I wrote a simple test webservice in .net but when I use the test page to try and run the webservice. IE does not recognize the output as an XML document, ie it doesn't automatically format the XML...
5
by: tenko | last post by:
anybody knows how to produce output restricted to just 2 decimal places? thanks :-)
7
by: Bo Yang | last post by:
Hi , I am reading some boost code now , and I got confused by the following code in the add_reference type trait . template <class TT&(* is_reference_helper1(wrap<T>) )(wrap<T>); char...
0
by: rn5a | last post by:
A custom server control containing a Button inherits from the WebControl class. I want to give users the option to change the BackColor of the Button. If this is the code in the ASPX page that uses...
2
Kosal
by: Kosal | last post by:
Dear All Can you help me to convert as below code to vb.net module Function GF_ListGroupsOfUser(ByVal stUserName As String) As String On Error GoTo err_ListGroupsOfUser Dim...
2
by: Leonidas Savvides | last post by:
HOW detect EOF at below code? what put in place of ( i<3306 ) ??? public mergerTxt() { try { File file = new File( "us-skype.lang" ); // get file name File fileRu = new...
0
by: javedshaikm | last post by:
Data from a XML sheet is converted to php array and I have tried to create a quiz format question with the below code, but it does't work. Not sure how to get it working for multiple questions... ...
2
by: prachi123 | last post by:
i have a table like as below; as table name temp SR ER x Y z 1 1 P Q R 2 5 l M N 6 9 S T G 10 10 Y O P I am looking a output as below :-
6
by: radha gogia | last post by:
#include<stdio.h> int f(); int main() { f(); return 0; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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...

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.