C program working and stop running at the same time? - c

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.

Related

Why does this piece of code freeze everytime?

// program to check if a number is a perfect square or not.
#include<stdio.h>
double perfect_square(double number)
{
step:
for (int i = 1; i<=number; i++)
{
if (i*i == number)
{
return number;
}
else
{
goto step;
}
}
return 0;
}
int main ()
{
double N;
printf("Enter a number: ");
scanf("%lf", &N);
double cube_decision = perfect_square(N);
if (cube_decision == 0)
{
printf("It is not a perfect cube");
}
else
printf("It is a perfect cube.");
return 0;
}
The above program lets me input a number but I it just kind of freeze and doesn't do anything onward. I tried replacing every double variables with int variable and made the function to return int type and it worked but my question here is why does this program suddenly freezes when I use double???
Note: This might not be the best program to check perfect square but still it would be of great help if anyone can find the mistake in this program! :)
Because if the condition i*i == number is false, then you start the loop all over again, from the very beginning (with the int i = 1 part). The goto will give you an infinite loop.
You don't nee the goto here, the loop will automatically iterate anyway:
double perfect_square(double number)
{
for (int i = 1; i<=number; i++)
{
if (i*i == number)
{
return number;
}
}
return 0;
}
And as a general rule, never use goto and labels.

i have a problem with my code and i'm not sure if i even wrote it correctly

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.

Stack limit exceeded due to repeated recursion

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);
}
}

Separate between numbers

I made this coding but I couldn't complete. The idea is should enter numbers and on the screen must shown as separately. Ex) If we enter:1234 result: 1 2 3 4.
#include<stdio.h>
int show_digit(int x);
int main(void)
{
int x;
printf("Enter the variable:");
scanf("%d", &x);
printf("%d ", show_digit(x));
return 0;
}
int show_digit(int x)
{
return show_digit(x / 10)%10;
}
You seemed to be trying to make show_digit() recursive, but you got stuck on how to actually do it. This refactored version of your function actually traverses from the last digit of the input to the first (which is the base case), and then starts printing out spaced digits as it comes out of the recursion. Note that I changed the return type of show_digits() to void because it is really now a utility function which does not compute anything.
void show_digit(int x)
{
if (x < 10)
{
printf("%d ", x);
return;
}
else
{
show_digit(x/10);
}
int the_digit = x % 10;
printf("%d ", the_digit);
return;
}
In this answer, I will not show you any code, but I will explain what's your issue.
I tried to compile your code, it compile successfully, but when I run it, the program crashed, is that your issue?
I know why your program crashed: Out of memory!
How did it happen?
Your function int show_digit(int) is a recursive function, in this function, you didn't made a statement to stop recursion, so, the recursion will continue until the function's stack reach maximum size of allowed memory, then it finally causes crashing.
Non-recursive version:
int digits[256];
int i = 0;
do {
digits[i++]=x%10;
x /= 10;
} while (x);
for (i=i-1; i>=0; i--) {
printf ("%d ", digits[i]);
}
It will work fine for positive integers.

Prime Palindrome Logic Efficiency

I wrote a code to get first 1000 prime palindromes ,though my logic is correct ,I dont seem to be getting first 1000 prime palindromes ,I am getting some 113 Prime Palindromes and after that I don't get any . I think this is because my logic is not efficient enough ,that is why it is taking so much time to compile ,but I already tried three different methods and everytime the Runtime is getting stuck after the 113th Prime Palindrome Number .
Can anyone explain why exactly I am getting this problem ,is it because the code is not efficient?
/* Program to find the first 1000 prime palindromes */
#include<stdio.h>
#include<math.h>
int prime(long int n)
{
int i,check=0;
if(n!=2 && n%2==0)
return 0;
if(n==2 || n==3)
return 1;
for(i=3;i<n/2;i=i+2)
if(n%i==0)
check++;
if(check==0)
return 1;
else
return 0;
}
/*long int reverse_number(long int n,long int partial)
{
if(n==0)
return partial;
else
return reverse_number(n/10,partial*10 + n%10);
}*/
int palindrome(long int n)
{
long int reverse = 0;
long int n_copy = n;
int rem;
while(n_copy!=0)
{
rem = n_copy%10;
reverse = reverse*10;
reverse = reverse + rem;
n_copy = n_copy/10;
}
if(reverse==n)
return 1;
else
return 0;
}
int main()
{
long int i;
int count=5,digits;
printf("The 1000 prime palindromes are: \n");
printf("1. 2\n2. 3\n3. 5\n4. 7\n");
for(i=11;;i=i+2)
{
if(prime(i))
{
if(palindrome(i))
{
printf("%d. %ld\n",count,i);
count++;
}
/*if(reverse_number(i,0)==i)
{
printf("%d. %ld\n",count,i);
count++;
}*/
}
if(count==50)
break;
}
printf("\n\n");
return 0;
}
Let me quote my imperative programming professor here: "You can check whether the number is a prime and a palindrome, or check whether it's a palindrome and a prime..."
Also, you're breaking the loop when count is 50, which I suppose you want to do when it's 1000.
Without editing the prime and palindrome functions, besides the order in which they're called, my PC stops finding more after 781 9989899.

Resources