Question

In: Computer Science

You have to write a program that will read an array from a file and print...

You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first line in the file provides the total number of values in the array. The subsequent lines will contain an integer value.

For example a sample input file “file1.txt” is:

3

397

73

47

Your output will be a yes/no for each value in the input file.

$./first file1.txt

no

yes

no

in C


language is C

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LSIZ 128
#define RSIZ 10

int checkPrime(int n){
   int i;
   for(i=2;i<n;i++){
       if(n%i == 0)
           return 0;
   }
   return 1;
}

int main(void)
{
char line[RSIZ][LSIZ];
   char yes[5] = "yes", no[5] = "no";
FILE *fptr = NULL;
int i = 0, n, flag = 1;
int tot = 0;

fptr = fopen("file1.txt", "r");
while(fgets(line[i], LSIZ, fptr))
   {
line[i][strlen(line[i]) - 1] = '\0';
i++;
}
fclose(fptr);
  
fptr = fopen("file2.txt", "w");
tot = i;
for(i = 0; i < tot; ++i)
{
   flag = 1;
n = atoi(line[i]);
printf("%d\n", n);
while(n!=0){
   if(!checkPrime(n)){
       flag = 0;
       break;
           }
           n = n/10;
       }
       if(flag == 1){
           printf("yes\n");
           fputs(yes, fptr);
       }
       else{
           printf("no\n");
           fputs(no, fptr);
       }
}
fclose(fptr);
return 0;
}


Related Solutions

C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them...
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them on the screen (each value on a separate line), you have to also save the grades (each value on a separate line) into another file “SalariesWithBonus.txt” after you add a bonus of two percent to each salary (example For the salary 100000 a bonus of 2000 will be added as 100000 X 0.02 = 2000). Your code should check whether the file “Salaries.txt” opened...
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
You are expected to write a program from scratch. In the program, an array will be...
You are expected to write a program from scratch. In the program, an array will be initialized with 23 random integers between 1000 and 1999 (inclusive). The output of the program is 4 lines on the screen, specifically, All elements in the array (line 1) All elements in reverse order (line 2) Every element that is less than 1500 and also at an odd index (line 3) Every odd element that is larger than 1500 (line 4) Note that you...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT