A set of numbers is stored in a file named numbers.txt. Write a program to copy all odd numbers to another file called odd.txt and all even numbers to a file called even.txt.
#include<bits/stdc++.h>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
int x=0;
ifstream fin;
ofstream fout1("odd.txt"),fout2("even.txt");
fin.open("numbers.txt");
string s;
while(!fin.eof())
{
fin>>s;
x=stoi(s);
if(x%2==0)
{
fout2<<x<<" ";
}
else
{
fout1<<x<<" ";
}
}
fin.close();
fout1.close();
fout2.close();
}
Comments
Leave a comment