Starting from:

$30

COM1002 Lab 5 -Solved

Write a C program which reads a set of numbers, un7l the EOF, from standard input (stdin) and prints the number of items read from the stdin and the sum of the digits of the prime numbers in the given set, in the given order. If there is no prime number in this set, you should print 0.  

Constraints: 

This week, you will be assessed on func7ons; so you must write two func7ons for this assignment:  

(1)  You must write a func7on that determines if a given number is prime or not.  

(2)  Also you must write another func7on that returns the sum of the digits of a given number.  

I/O format: 

input:  <set of integers> output: <sum of the digits of the prime numbers><SPACE><# of items read>

Example:

Sample Input: 2 4 3 10 8 11 9 13 19 the set of prime numbers, in the given order: {2, 3, 11, 13, 19}  

Sum of the digits of the given primes: {2, 3, 2, 4, 10}

 # of integers read: 9

Output: 2 3 2 4 10 9

Hint: Remember that EOF is a predefined macro which stands for End Of File. You can capture EOF using scanf func7on as follows:

int x; 

while(scanf(“%d”,&x) != EOF) 

                {               <do domething>             } 

SuggesSon: 

Prepare sample input files, i.e. input.txt, to test your program. Assume that your executable is lab5. Test your programs like this:

               >./lab5<input.txt

More products