C++ program that reads an integer array from a file and finds all integers which start and end with same digit. Write these integers into an output file. you may assume the length of each integer to be a minimum of 2 digits.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
fstream file, file2;
file.open("input.txt", ios::in); //array saved as: 23 65 87 44 595 878 648 323 425 22
file2.open("output.txt", ios::out | ios::trunc);
int a;
if(file) while(file>>a){
string s = to_string(a);
if(s[0] == s[s.length() - 1]) file2<<a<<" ";
}
file.close();
file2.close();
return 0;
}
Comments
Leave a comment