Question

Write a C program that reads a list of positive integers from a file named "input.txt."...

Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

code:

#include<stdio.h> int main() {   FILE *f;                f=fopen("input.txt","r"); // file input         int a;  int counter_odd=0;      int counter_even=0;     while(fscanf(f," %d",&a)==1) //scanning integers and counting no. of odds and evens         {        if(a%2==0)             counter_even++;                 else            counter_odd++;  }       fclose(f);      FILE *o;        o=fopen("results.txt","w");     fprintf(o, "no. of even integers: %d\n", counter_even); // output to results.txt        fprintf(o, "no. of odd integers: %d\n", counter_odd);   fclose(o);       }

input.txt

 8 10 11 12 13 14 15 16 17 18

results.txt

no. of even integers: 6 no. of odd integers: 4 

instructions:-

1) you just have to create an input.txt file on the location where your program is saved

2) results.txt file will be created automatically by the program.

output:-

1)input.txt

File Edit Search View Encoding Language Settings Tools Macro Run Plugins Window ? DE BOLO PAGE 21 FONDO CON Binput txt X resu2)results.txt

File Edit Search View Encoding Language Settings Tools Macro Run Plugins Window ? 8. T OP GEE 1 SOODNE Binput txt > results.t

Please upvote if you like the answer!

Add a comment
Know the answer?
Add Answer to:
Write a C program that reads a list of positive integers from a file named "input.txt."...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT