Program for GCD

C programming help for GCD of 2 integers

Page 1 of 1

4 Replies - 8404 Views - Last Post: 17 February 2009 - 05:51 PM Rate Topic: -----

#1 Dabucs78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 09

Program for GCD

Posted 17 February 2009 - 01:27 PM

I am trying to write a program for GCD of 2 integers. I have compiled my program with no errors or warnings using Sunblast. The problem is I am not getting the output that I need. It is asking me for another input after my first input and it is not recognizing the 3 in the y input. I also attached a copy of the output at the bottom of this code. Can someone Please Help me out since I cannot figure out what I am doing wrong. Thank you very much for you help and your time...
#include <stdio.h>
int GCD (int x, int y) 
{
  int b;
  if((x%y) == 0)
  {
	b = y;
  }
  else if((y%x) == 0)
  {
	b = x;
  }
  else
  {
	GCD(y, x%y);
  }
  return b;
}
int get_input()
{
  int a, i=0;
  scanf("%d\n", &a);
  if(a<=0) 
  { 
	for (i = 0; a <= 0; i++)
	{
	  printf("Please enter an integer greater than 0\n");
	  scanf("%d\n", &a);
	}
  }
  return a;
}
int main()
{
  int x, y, z;
  printf("This program computes the greatest common divisor\n");
  printf("of positive integers x and y, entered by the user\n");
  printf("Inputs must be integers greater than zero\n\n");

  printf("Enter integer x:");
  x = get_input();
  printf("Enter integer y:");
  y = get_input();
  z = GCD(x,y);
  printf("The GCD of %d and %d is %d\n", x, y, z);
  return 0;
}


----------------------------------------------OUTPUT-------------------------------------------------------------- with example numbers input highlighted. This is exactly how it appears on SSH(sunblast)
This program computes the greatest common divisor
of positive integers x and y, entered by the user
Inputs must be integers greater than zero

Enter integer x:6
4
Enter integer y:3
The GCD of 6 and 4 is 4

This post has been edited by Dabucs78: 17 February 2009 - 03:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Program for GCD

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Program for GCD

Posted 17 February 2009 - 02:07 PM

Please use code tags when posting code in the future.

Try this:
int get_input() // <--Function Definition
{
  int a, i=0;
  scanf("%d\n", &a);
  getchar(); // Eat leftover newline
  if(a<=0) // this if statement leads to a loop that will loop if the user inputs 0 or less, though i think the project guide lines say this is suppose to be a recursive loop,
  { // meaning call itself until the right valid input is entered.
    for (i = 0; a <= 0; i++)
    {
      printf("Please enter an integer greater than 0\n");
      scanf("%d\n", &a);
      getchar(); // Eat leftover newline
    }
  }
  return a;
}


Was This Post Helpful? 0
  • +
  • -

#3 Dabucs78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 09

Re: Program for GCD

Posted 17 February 2009 - 03:21 PM

int get_input()
{
  int a, i=0;
  scanf("%d\n", &a);
  getchar();
  if(a<=0) 
  { 
	for (i = 0; a <= 0; i++)
	{
	  printf("Please enter an integer greater than 0\n");
	  scanf("%d\n", &a);
	  getchar();
	}
  }
  return a;
}



Alright, I added your suggestion, but it actually created a new problem.
Here is the new change that works but not as i need it to. I'm wondering if there is something I am missing in C programming that seems to only appear with the x variable.
------------------------Output-----------------------------

This program computes the greatest common divisor
of positive integers x and y, entered by the user
Inputs must be integers greater than zero

Enter integer x:6
4
Enter integer y:3
2
The GCD of 6 and 3 is 3

So the change did make the program run somewhat more properly, it is now taking the X and Y variable at the right time and using them properly. But i really need that space to be removed altogether. But all 4 openings now require something to be entered. I thought the getchar() lines worked so as to i could just press enter to move on, but it requires me to enter in something to move on, Was I wrong on that?

Thank you for any help given.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Program for GCD

Posted 17 February 2009 - 04:41 PM

Get rid of the \n in your scanf format string.
Was This Post Helpful? 1
  • +
  • -

#5 Dabucs78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 09

Re: Program for GCD

Posted 17 February 2009 - 05:51 PM

Thank You, that fixed the problem.

I have one more problem that I cannot seem to understand. I need the program to check the input given to check and see if it is valid input. It should not accept letters but it can check doubles. aka abc results in the error message stating please enter an integer greater than 0.

But if 123.456 is entered it should accept that as 123. Current when a decimal is entered it sets both x and y as the integer before the decimal place, ex: 123.456 -> x = 123, and y = 123.

int get_input()
{
	int a, i = 0;
	scanf("%d", &a);
	if(a <= 0.00)
	{
		for (i = 0; a <= 0; i++)
		{
		printf("Please enter an integer greater than 0\n");
		scanf("%d", &a);
		}
	}

	return a;
}



-----------------Output-----------------
This program computes the greatest common divisor
of positive integers x and y, entered by the user
Inputs must be integers greater than zero

Enter integer x:123.456
Enter integer y:The GCD of 123 and 123 is 123

This program computes the greatest common divisor
of positive integers x and y, entered by the user
Inputs must be integers greater than zero

Enter integer x:abc
Enter integer y:The GCD of 65588 and 65588 is 65588
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1