I am new to Programming, just a student.
I have written a program to calculate the GCD of two numbers using recursive function, but it's giving the correct answer for some, while it gives wrong answer for a few others. Kindly help me in identifying the problem:
#include<stdio.h>
#include<conio.h>
int gcd(int,int,int)
int main(){
int a,b,x,val;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
if(a>b)
x=b;
else
x=a;
val=gcd(a,b,x);
printf("The GCD of the two numbers you entered is:%d",val);
getch();
return 0;
}
int gcd(int a,int b,int x){
if(a%x==0){
if (b%x==0)
return x;
}else
return gcd(a,b,x-1);
}
For example, the program gives a wrong answer when first number = 69, second number = 65, whereas in some other cases it mysteriously gives the right answer.
Can someone help me out here?
Check your code path. Not all condition return an integer in the gcd function.
Which compiler are you using? It should give you a warning or error.
Try this:
int gcd(int a,int b,int x){
if(a%x==0 && b%x==0) return x;
}else return gcd(a,b,x-1);
}
This catches both the modulus comparisons to 0 in one if statement, all conditions not falling within this get gcd calls.
Example: consider the numbers a=100 and b=44. When it reaches x=25, which divides 100, but not 44, your code doesn't have a proper path to take.
You are not taking all the conditions (paths) into consideration.
int gcd(int a,int b,int x){
if(a%x==0){
if (b%x==0)
return x;
else
// ERROR ERROR ERROR,
// if the code reaches here, then it neither calls gcd recursively,
// nor does it return anything valueable
}else
return gcd(a,b,x-1);
}
You should change the code as below.
int gcd(int a,int b,int x){
if(a%x==0)
if (b%x==0)
{
return x;
}
return gcd(a,b,x-1);
}
OR
int gcd(int a,int b,int x){
if(a%x==0 && b%x==0) return x;
return gcd(a,b,x-1);
}
GCD of two numbers in C (the easiest way):
while(secNum != 0) {
Temp = fNum % secNum;
fNum = secNum;
secNum = Temp;
}
printf("GCD of the given two numbers : %d",fNum);
Related
I'm not able to code a recursive function for this, I have given the example after the program,
#include<stdio.h>
int sum(int x);
int main()
{
int n,s;
printf("enter the five digit number whose digits need to be added");
scanf("%d",&n);
s= sum(n);
printf("The sum of all the digits of a five digit number is %d",s);
}
int sum(int x)
{
int d=0,a;
for(i=1;i>=5;i++)
{
a=x%10;
x=x/10;
d= d+a;
}
return(d);
}
The following is the recursive code for the above program that I coded myself,
#include<stdio.h>
int sum(int x);
int main()
{
int n,s;
printf("enter the five digit number whose digits need to be added\n");
scanf("%d",&n);
s= sum(n);
printf("The sum of all the digits of a five digit number %d",s);
}//This is my poor try inspired by coderedoc
//please fix this code my laptop battery gonna be over already
int sum(int x)
{
int d=0, a;
if(x<0)
return sum(-x);
else
a= x%10;
x= x/10;
d=d+a;
return sum(x);
else
if(x==0)
return d;
}
int sum(int x){
if( x < 0) return sum(-x);
return (x==0)?0:(x%10)+sum(x/10);
}
The code will be as simple as this. If you reach the x=0 state you are done. Else add the last digit with the sum of the rest of the digits.
Also when building your solution - try to make it generalize to some extent. 5 digit is good but think if you can extend it for more numbers of digits. This works for that too.
int sum(int x){
if(x < 0) return sum(-x);
if(x == 0)
return 0;
else
return (x%10)+sum(x/10);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
this code is showing some syntactical errors I can't find out where it is, may be it is in the gcd function. here is my code.
#include<stdio.h>
#include<conio.h>
int main(void)
int gcd(int,int);
{
int a,b, temp;
printf("Enter the value of a");
scanf("%d",&a);
printf("Enter the value of b");
scanf("%d",&b);
if (b>=a)
{
int temp;
temp=b;
b=a;
a=temp;
}
elseif(b!=0)
{
gcd(a,b);
printf("The Gcd of the numbere is %d",gcd(a,b));
}
else
{
printf("The GCD of %d %d is %d:",a,b,a);
}
getch();
return 0;
}
int gcd(int a,int b)
{
int g;
while(b!=0)
{
g=a%b;
a=b;
b=g;
return g;
}
}
I would be thankful if you point out my errors and explain with the correct code.
switch the position of these two lines:
int main(void)
int gcd(int,int);
also, elseif -> else if
The gcd function uses Euclid's Algorithm. It computes a mod b, then swaps a and b with an XOR swap.
Reference
#include<stdio.h>
int gcd(int ,int );
int main(void)
{
int a,b, temp;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
int res = gcd(a,b);
printf("The Gcd of the numbere is : %d \n",res);
return 0;
}
int gcd(int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
I've started learning C and programming in general relatively recently and we were told to make a program that finds the GCD and LCM of two numbers using recursion.
Now after some grinding I managed to get this together.
#include<stdio.h>
int gcd(int a,int b);
int main()
{
int a,b,l,temp;
printf("Enter two numbers :\n");
scanf("%d%d",&a,&b);
if(a<b)
{
temp=a;
a=b;
b=temp;
}
l=gcd(a,b);
printf("GCD = %i\nLCM = %i",l,a*b/l);
return 0;
}
int gcd(int a,int b)
{
static int c;
c=a%b;
a=b;
b=c;
if(c!=0)
{
gcd(a,b);
}
else
return a;
}
Now for some reason unknown to me, the function does not work without the "else". More specifically like this:
int gcd(int a,int b)
{
static int c;
c=a%b;
a=b;
b=c;
if(c!=0)
{
gcd(a,b);
}
return a;
}
Although irrelevant to the assignment I feel like I should understand what the problem here is. As a novice I would appreciate any and all help.
I apologize in advance if the question is too stupid or the code too messy.
The problem is the recursive call:
int gcd(int a,int b)
{
static int c;
c=a%b;
a=b;
b=c;
if(c!=0)
{
gcd(a,b); // The problem is here
}
else
return a;
}
You have done two different approaches:
In the case above you don't have a return statement if c!=0. Your function has to return int. Normally compilers give you a warning because you will return a kinda random number see here. So let's say it is luck that your function with else works.
Without the else statement you will always return a. You calculate gcd but you will never use the result so your result of the first call will always be the smaller number between the a and b of your main. You need to use the result of your recursive call to make the function working.
The right approach is to return your result of the recursive call like Sanjay-sopho already said:
return gcd(a,b);
Additionally it is bad coding style to use braces on the if and no braces on the else ;) Both cases are fine but keep it identical.
You asked about problems in your code. Here,
static int c;
why is it static, Furthermore you don't need third variable to calculate gcd using recursion.
And,
gcd(a,b);
In what variable are you returning gcd. This doesn't make sense at all. It's not a void function, it's returning an int.
Now the correct method,
if (b != 0)
return gcd(b, a%b);
else
return a;
That's it.
in "C" make it like this,
make a method to Take two number and find the gcd is name gcd
int gcd (int a,int b){
if (b==0)
return a;
else return gcd(b,a%b);
in the main i will colling by
printf("G.C.D OF %d AND %d is : %d ",a,b,gcd(a,b));
I use a recursiv functions
I have an array of size n. I need to find the GCD of each element with a given number and if it's greater than 1, add it to another array. What's the fastest way to do this?
int gcd(int a, int b)
{
if(b == 0) {
return a;
}
else {
return gcd(b, a % b);
}
}
For small numbers use binary GCD (which is faster Euclid's GCD algorithm) & for large numbers try Lehmer's algorithm.
Binary GCD: https://www.geeksforgeeks.org/steins-algorithm-for-finding-gcd/
The following code uses the normal method that we humans use to calculate the GCD and is by far, according to me the fastest way to find GCD(HCF) of 2 numbers:
#include <iostream>
using namespace std;
int main()
{
int N1;
int N2;
cin<<N1;
cin<<N2;
int small=N1;
int big=N2;
if (N1>N2){
N1=small;
N2=big;
}
int P=1;
int i=1;
while (i<=N1){
if (N1%i==0 && N2%i==0){
N1=N1/i;
N2=N2/i;
P=P*i;
i=1;
}
i=i+1;
}
cout<<"GCD= "<<P;
return 0;
}
#include<stdio.h>
int main()
{
int a,b,a1,b1;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
a1=a;b1=b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
printf("GCD of %d and %d is %d\n",a1,b1,a);
printf("LCM of %d and %d is %d",a1,b1,(a1*b1/a));
}
I am learning recursion and i encountered a conceptual doubt while solving the problem of calculation of remainder when a positive integer is a is divided by a positive integer b.
My code is:
#include<stdio.h>
#include<stdlib.h>
int x;
int rem(int a,int b)
{
x=a;
if(x>=b)
{
x=x-b;
rem(x,b);
}
printf("%d\n",x);
return x;
}
int main()
{
int a,b;
printf("Enter a & b\n");
scanf("%d %d",&a,&b);
int y =rem(a,b);
printf("rem is :%d",y);
return 0;
}
Its working fine. I have learned that for every call a new set of formal parameters and local variables are created.
So i experimented it by printing x on return of every recursive call!
But it is printing 1 1 1 1. Why is the value of x corresponding to a particular call not printed. ?
Why only the last modified value printed?.. Is that because i declared 'x' as global?
In this case perhaps you need only to move your print up
int rem(int a,int b)
{
x=a;
printf("%d\n",x);
if(x>=b)
{
x=x-b;
rem(x,b);
}
return x;
}
But I think you should avoid the use of global variables in a recursive alrotithm. It could make the algorithm very difficult to reason about. Recursive functions are better to be 'pure functions'.
It is because while x >= b, rem() is repeatedly called before printf()s are called. Only after x < b will the printf()s are called as each call on rem() unwinds.
You might want to make x local to rem() to get the desired result.
Ignoring issues with checking the return value from scanf() and that the two entered values are both positive, etc, I think you can and should avoid x altogether. You could use:
#include <stdio.h>
static int rem(int a, int b)
{
if (a >= b)
a = rem(a-b, b);
printf("%d\n", a);
return a;
}
int main(void)
{
int a, b;
printf("Enter a & b\n");
scanf("%d %d", &a, &b);
int y = rem(a, b);
printf("rem(%d, %d) is: %d\n", a, b, y);
return 0;
}
This code captures the return value from rem() at each level of recursion. In this case, because the returned value doesn't change as the recursion unwinds, you could use the global variable x, but there is no need for it, and you should avoid global variables whenever you can.
#include<stdio.h>
#include<conio.h>
int fun(int,int);
int main()
{
int a,b;
printf("enter two numbers");
scanf("%d %d",&a,&b);
fun(a,b);
//printf("%d",fun(a,b));
}
int fun(int a,int b)
{
if(a<b)
printf("%d",a);
if(a>=b)
a=fun(a-b,b);
return a;
}