Answer to Question #190673 in C++ for Shujaa

Question #190673

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.


1
Expert's answer
2021-05-08T03:10:47-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS