This code is to print the Fibonacci series using recursion. So I thought to devise a recursion instead of using iteration but as soon as I just execute the code and as soon as the value provider function is executed it is showing some error "segmentation error". I want to do it this way only... Can anyone help? I'm just a beginner so please help and encourage me...
#include<stdio.h>
int fibonacci(int n)
{
int res;
if(n==0)
return 0;
if(n==1)
return 1;
else
res = fibonacci(n-1)+fibonacci(n-2);
return res;
}
int value_provider(int n)
{
int choice1;
if(n>=0)
{
choice1 = fibonacci(n-1);
n -- ;
}
printf("%d",choice1);
if(n>=0)
{
value_provider(n);
}
}
void main()
{
int n;
printf("enter the number");
scanf("%d",&n);
value_provider(n);
}
This code is showing segmentation fault...
What can I do to remove it rather than changing the code?
I want to do it only this way; please help!
I think your value_provider function has poor terminating conditions.
Don't try to calculate fibonacci of -1 so n must be >=1
Also once n is zero you need to finish the recursion, don't
call value_provider again.
Try something more like this;
int value_provider(int n)
{
int choice1=-1;
if(n>=1)
{
choice1 = fibonacci(n-1);
n -- ;
}
printf("%d ",choice1);
if(n>0)
{
value_provider(n);
}
}
Related
so i'm practicing c and i built a program that says if its prime number or not and i tried to execute it but it wont work it doesnt shows me the output oh and im still new to this i started learning c one week ago.
i dont know how to fix this.
#include <stdio.h>
void Num();
int main()
{
void Num();
return 0;
}
void Num()
{
int n, i, flag = 0;
printf("Enter a num: ");
scanf("%d", &n);
for(i = 1; i <= 10; i++)
{
for(n = 1; n <= 10; n++)
{
flag = 1;
}
}
if( flag == 1)
{
printf("its not the prime num ");
} else{
printf("its the prime num" );
}
}
it wont even show the printf output
You need to go back to the basics (this means: reading a good C book before diving in). You are confusing declaration and calling of functions.
int main()
{
void Num();
return 0;
}
main contains two statements:
A local (re)declaration of Num as a function without return value.
A return statement.
Since you want to call Num rather than redeclaring it, you need to use the function call syntax:
int main()
{
Num();
return 0;
}
This is just the first step, however. Your Num function does not perform the correct actions to determine primality.
Now I am practicing using Vim on Linux.
I made a simple code like this
#include <stdio.h>
int factorial(int n){
if (n<0) { return 0; }
else if (n==0) { return 1; }
else { return n * factorial(n); }
}
int main(void){
int n = 0;
printf("Put n value : ");
scanf("%d", &n); /* non-OP inserted ";" to help people focus on the problem */
printf("%d! = %d\n", n, factorial(n));
return 0;
}
When I put -1 and 0, it works. They return 0 and 1.
However, when I put positive integer values on n, it didn't work.
I tried to find out the reason so I used gdb,
but it just said like this :
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400620 in factorial ()
What's wrong with my code? I even cannot understand the point.
When n > 0 your recursive program never terminates. The value of n is never decremented and so it continues running recursively until your run out of memory.
It should be return n * factorial(n-1);
You code is causing stack overflow. In the given function n is never decremented. Last statement should be
else { return n * factorial(n-1); }
How can I run this program without any crash #C #CoFactors
This program is about finding cofactors of a number it is working but at the same time this program crashes after showing the output. Please take a look.
Here is the program.
int coFactors(int number,int divisor)
{
if(number%divisor==0)
{
printf("%d ",divisor);
number /= divisor;
coFactors(number,divisor);
}
else if(number==divisor-1)
{
return;
}
else
{
coFactors(number,++divisor);
}
}
int main()
{
int num;
printf("Enter number:");
scanf("%d",&num);
coFactors(num,2);
return 0;
}
seems below condition is wrong:
else if(number==divisor-1)
it should be:
else if(number < (divisor))
My earlier post would have avoided crashed but to get correct Co factor condition should have been like edited.
BLUEPIXY has suggested correctly.
I am trying to calculate factorial using recursion but my program is returning wrong value. I am unable to understand the recursion functionality. Please help me in understanding how recursion works. My code is as follows:
#include <stdio.h>
#include <math.h>
int main()
{
//code
int T,N,sol;
scanf("%d\n",&T);
while(T--) {
scanf("%d\n",&N);
sol=fact(N);
printf("%d\n",sol);
}
return 0;
}
int fact(int n)
{
int value;
while(n>0) {
value=n*fact(n-1);
n=n-1;
}
return value;
}
Replace your fact function with this one:
int fact(int n)
{
int value = 1;
if(n>0)
{
value=n*fact(n-1);
}
return value;
}
You are using recursion as well as a while loop in the function fact
A recursion should replace the loop. You also need an exit condition for the function. For the factorial it can be at 0 and the factorial of 0 is 1.
The function can be rewritten as below
int fact(int n)
{
int value;
if (n <0)
{
return -1;
}
else if (n == 0)
{
return 1;
}
else
{
value=n*fact(n-1);
}
return value;
}
Edited to add condition for negative numbers. If the function returns -1, then the main program should give the user an error message.
I've tried the Marbles pkroblem on spoj - (link: http://www.spoj.com/problems/MARBLES/ )
However I'm getting Runtime Error(SIGSEGV) after multiple tries.
Here's the code I submitted -
#include <stdio.h>
int comb(long int n, long int k)
{
if (n==k || k==0)
return 1;
else
return (comb(n-1, k) + comb(n-1, k-1));
}
int main() {
int t;
long int n, k;
scanf("%d", &t);
while(t--)
{
scanf("%ld %ld", &n, &k);
printf("%d\n", comb(n-1, k-1));
}
return 0;
}
I cant understand why I'm getting the error. Other solutions on the internet have all used iterative way to calculate the nCr combination. I've tried using a recursive approach. Any help would be highly appreciated.
you are decrementing n in the recursion but you don't check the case when n becomes 0 or negative, so the segfaul basically tells you that you get into an infinite recursion.
just add some stop criterion in addition to n==k || k==0.
I didn't look at the problem description so I don't know what the correct condition is, but perhaps you need something like
if (n==k || k==0 || n==0)