Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a program as follows: #include <iostream> #include <fstream> using namesp

ID: 3670073 • Letter: I

Question

I have a program as follows:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int a,b,noOfDivisors=0,sumOfDivisors=0,sumOfAll=0,largest,smallest;
float average;
ifstream myfile;
myfile.open("Integers.dat");
myfile >> a;
myfile >> b;
largest=smallest=a;

for(int i=a; i<=b; i++)
{
sumOfAll+=i;
if((i%5==0)||(i%6==0))
{
noOfDivisors++;
sumOfDivisors+=i;


if(smallest>i)
{
smallest>i;
}
if(largest<i)
{
largest=i;
}
}
}
myfile.close();

cout <<"Low: "<<a<<" High : "<<b<<endl;
cout<<"Number of integers divisible by 5 OR 6:"<<noOfDivisors<<endl;
cout<<"Sum of integers divisible by 5 OR 6:"<<sumOfDivisors<<endl;
cout<<"Sum of all integers :"<<sumOfAll<<endl;
cout<<"Largest Integer :"<<largest<<endl;
cout<<"Smallest Integer :"<<smallest<<endl;
cout<<"Average of Largest and Smallest :"<<((largest+smallest)/2.0)<<endl;

return 0;
}


Integers.dat
100 200

I need it to find the numbers divisible by '5 or 6 NOT both'

Explanation / Answer

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int a,b,noOfDivisors=0,sumOfDivisors=0,sumOfAll=0,largest,smallest;
ifstream myfile;
myfile.open("Integers.dat");
myfile >> a;
myfile >> b;

largest=smallest=a;

for(int i=a; i<=b; i++)
{
sumOfAll+=i;
if(((i%5==0)||(i%6==0)) && ((i%5!=0)&&(i%6!=0)))
{
   noOfDivisors++;
   sumOfDivisors+=i;

   if(smallest>i)
   {
       smallest=i;
   }
   if(largest<i)
   {
   largest=i;
   }
}
}
myfile.close();

cout <<"Low: "<<a<<" High : "<<b<<endl;
cout<<"Number of integers divisible by 5 OR 6:"<<noOfDivisors<<endl;
cout<<"Sum of integers divisible by 5 OR 6:"<<sumOfDivisors<<endl;
cout<<"Sum of all integers :"<<sumOfAll<<endl;
cout<<"Largest Integer :"<<largest<<endl;
cout<<"Smallest Integer :"<<smallest<<endl;
cout<<"Average of Largest and Smallest :"<<((largest+smallest)/2.0)<<endl;

return 0;
}