Suppose a file named “Num.txt” contains a list of integers. Write a program to extract the prime numbers only from that file and write them on “Prime.txt” file.

This answer is restricted. Please login to view the answer of this question.

Login Now

Program to read prime numbers from file “Num.txt” and write it to “Prime.txt” file is

#include <stdio.h>

int is_prime(int);

int main(){

    FILE* ptr;
    ptr = fopen("Num.txt", "r");

    FILE* fp;
    fp = fopen("Prime.txt", "a+");

    if (NULL == ptr) {
        printf("File can't be opened \n");
        return 0;
    }

    int num;
    printf("\nPrime number in files are:\n");
    while (fscanf(ptr, "%d", &num) != EOF){
        if( is_prime( num ) ){
            printf("%d\n", num);
            fprintf(fp, "%d ", num);
        }
    }

    return 0;
}

int is_prime( int n ){

    if( n == 1 ){
        return 0;
    }

    int j, flag = 1;

    for (j = 2; j <= n / 2; ++j) {
        if (n % j == 0) {
            flag = 0;
            break;
        }
    }

    return flag;
}
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .