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.
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
2)results.txt
Please upvote if you like the answer!
Write a C program that reads a list of positive integers from a file named "input.txt."...
1. use c++ write a c++ program that reads 10 integers from a file named input.txt in this file the 10 integers wil be on a single line with space between the. calculate the average of these numbers and then write his number to a file named output.txt. if the program is unable to successfully complete the file operations then display eero message to the console.
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
C++ Programming question
Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...
C++
3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...
C++ Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.
How to write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt." Sample input 10 12 1 3 5 34 2 5 7 9 44
C++ assignment Write a program that reads a sequence of integers and prints the following: 1. The number of odd and even numbers of inputs Use a sentinel value to signal the end of inputs. If the sentinel value is the first you enter, give a message “NO input is entered!”. Input Validation: Do not accept a negative number.
Please use Python to solve this problem: Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, on a single line and separated by a single space, the sum of all the even integers read and the sum of all the odd integers read.
In C++ write an application which takes positive or negative integers and returns the sum of each place in the number, such that 246 results in 12 and -88 results in -16. The program is called and runs with no prompt for input. The program reads exactly one signed integer from standard input. The program displays the sum of each digit of the number as a signed integer. This should be done in two files called main.cc and makefile.The result...
write a short c++ program that reads integers from a file named zzin1.dat (5,7,21,2,-9,10,3,1,11) until the eof condition is reached add 1 to each value and save it to an output file named acdc1.dat