3

This code is supposed to check if a user-inputted number is a prime number or not. I am executing the program on the cygwin terminal, and whenever I run it and enter a number, it says, "Segmentation fault (core dumped)". Any suggestions?

#include <stdio.h>

int prime(int num, int i, int count);

void main()
{
    int num, i=2, count=0, result;

    printf("Please enter a number: ");
    scanf("%d", &num);

    result = prime(num, i, count);

    if (result != 0)
        printf("num is not a prime number");
    else
        printf("num is a prime number");
}

int prime(int num, int i, int count)
{
    if (i < num)
    {
        if (num%i == 0)
        {
            count++;
            prime(num, i++, count);
        }
        else
            prime(num, i++, count);
    }
    return count;
}
Michael
  • 3,093
  • 7
  • 39
  • 83
  • A small tip, if a number is divisible by 2 (i.e it is even) you can skip it, except 2 of course. 2 is the only even prime. – Skurmedel Oct 15 '15 at 04:23

3 Answers3

4

You use post increment i++ in your function parameter. This absolutely does nothing. Because the post increment occurs after execution. So your i variable is never incremented and makes infinite recursion which gives you segmentation fault.

You can fix it with pre-increment ++i or call function with i+1.

int prime(int num, int i, int count)
{
    if (i < num)
    {
        if (num%i == 0)
        {
            count++;
            prime(num, ++i, count);
        }
        else
            prime(num, ++i, count);
    }
    return count;
}
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
2

Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” It’s a helper mechanism that keeps you from corrupting the memory and introducing hard-to-debug memory bugs. Whenever you get a segfault you know you are doing something wrong with memory – accessing variable that has already been freed, writing to a read-only portion of the memory, etc. Segmentation fault is essentially the same in most languages that let you mess with the memory management, there is no principial difference between segfaults in C and C++.

There are many ways to get a segfault, at least in the lower-level languages such as C(++). A common way to get a segfault is to dereference a null pointer:

for more better to go through these links: What is a segmentation fault?

what is Segmentation fault (core dumped)?

Community
  • 1
  • 1
Roushan
  • 4,074
  • 3
  • 21
  • 38
0

Using recursion to print prime numbers

public class PrimeNumberUsingRecursionFun {

static int count = 0, i = 1, j = 1, n = 2;

public static void prime() {
    if (i < 100) {
        if (n % j == 0) {
            count++;
            j++;

        }  else if (count == 2 && j > n) {
            System.out.print(n + "  ");
            n++;
            j = 1;
            count = 0;
            i++;
        }
        else if (count == 1 && j < n) {
            j++;
        }
        else if(count >= 2) {
            j = 1;
            count  = 0;
            i++;
            n++;

        }
        prime();
    }


}

public static void main(String[] args) {
    prime();
}

}