include <cmath> #include <iostream> using namespace std; int main(int argc, char
ID: 3623679 • Letter: I
Question
include <cmath>#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int n = 1234;
int d1 = n % 10; n = n / 10;
int d2 = n % 10; n = n / 10;
int d3 = n % 10;
int d4 = n / 10;
cout << d1 << d2 << d3 << d4;
int n2 = d1 + d2 + d3 + d4;
cout << n;
cout << n2;
cout << n + n2;
cout << endl;
return 0;
}
Modify the program to:
1. read the number from a file
2. compute the last four digits of the number
3. compute the reversed number of the last four digits of the number
Suppose the number is 123456789
The last four digits are 9,8,7,6 and the reversed number of the last four digits is 9876
4. add the reversed number to the original number
5. write the original number, the last four digits, the reversed number and the sum of the original number and the reversed number to a file
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
using namespace std;
int main()
{int n,orig,sum;
ifstream in;
ofstream out;
char filename[20];
cout<<"Enter input file name";
cin>>filename;
in.open(filename);
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
cout<<"Enter output file name";
cin>>filename;
out.open(filename);
in>>n;
orig=n;
int d1 = n % 10; n = n / 10;
int d2 = n % 10; n = n / 10;
int d3 = n % 10; n=n/10;
int d4 = n % 10;
int n2 = d1*1000 + d2*100 + d3*10 + d4;
sum=n2+orig;
out<<"original number: "<<orig<<endl;
out<<"last 4 digits: "<<d1<<" "<<d2<<" "<<d3<<" "<<d4<<endl;
out<<"last 4 digits reversed: "<<n2<<endl;
out<<"sum of last 4 digits and original number: "<<sum<<endl;
out.close();
in.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.