Reputation: 149
The program is very simple , it gives the greatest common divisor as the output.I have verified my algorithm.The compiler issues no error ,but still it wont produce any output.
#include<conio.h>
#include <stdio.h>
int gcd(int ,int );
int main()
{
int a,b,j;
printf("enter two numbers");
scanf("%d\n",&a);
scanf("%d\n",&b);
j=gcd(a,b);
printf("gcd is %d",j);
getch();
return 0;
}
int gcd(int x, int y)
{
int temp,c;
if(x<y)
{
temp=x;
x=y;
y=temp;
}
if(y<=x&&(x%y==0))
return y;
else
{ temp=x%y;
c=gcd(y,temp);
return c;
}
}
Upvotes: 1
Views: 2650
Reputation: 40155
scanf("%d\n",&a);
scanf("%d\n",&b);
to
scanf("%d%*c",&a);
scanf("%d%*c",&b);
Upvotes: 0
Reputation: 4325
The problem is
scanf("%d\n",&a);
scanf("%d\n",&b);
Delete the \n
, just
scanf("%d",&a);
scanf("%d",&b);
is OK
Upvotes: 1
Reputation: 471559
This could be due to buffering of the output. Add \n
to your printfs
and see if it fixes it:
printf("enter two numbers\n");
printf("gcd is %d\n",j);
Alternatively, you can add calls to fflush(stdout)
to flush the output buffer:
printf("enter two numbers");
fflush(stdout);
printf("gcd is %d",j);
fflush(stdout);
Other than that, it (almost) works as intended on my setup:
enter two numbers
4783780
354340
1
gcd is 20
The only thing is that the \n
forces it to read an extra character. (which I chose to be 1
)
Upvotes: 2
Reputation: 272802
This line:
printf("enter two numbers");
doesn't print a newline character (\n
), and so the output isn't flushed to the console.
Try adding this after the printf
:
fflush(stdout);
Upvotes: 0