Write a program to read from “data.txt” that contains more than 1 digit of integers. Then you have to find the number which have same digits like (444, 3333, 11). You should make as many functions as you can with proper naming convention.
#include<iostream>
#include <stdio.h>
#include <string>
using namespace std;
main(void)
{
fstream infile;
int x,Sum;
string Name = "H:\\Data.txt";
infile.open(Name.c_str());
if (!infile)
{
cout << "Unable to open file: "<<Name;
exit(1); // terminate with error
}
else
{
while(infile >> x)
{
string str; // a variable of str data type
stringstream ss;
ss<< x; ss >> str;
Sum=0;
for(int r=0;r<str.length();r++) Sum = Sum + str[r]-str[0];
if(Sum==0) cout<<x<<endl;
}
infile.close();
}
}
Input File
Output:
Comments
Leave a comment