How can I get this code to: a.) Output results to a file called “Lastname_calcRe
ID: 3670542 • Letter: H
Question
How can I get this code to:
a.) Output results to a file called “Lastname_calcResults.txt”
b.) Make sure you check for divide by 0
c.) Tell which number is larger or if they’re equal
d.) Tell if each number is odd or even
Do this for just the integers, not the decimal numbers.
// Mark
// February 15, 2016
// Program 1 Simple Calculator
#include <iostream>
using namespace std;
int main()
{
int a,b; // Declare Variables
double c,d;
cout << "Input two numbers" << endl;
cin >> a >> b; // Input two integers
cout << "Sum = " << a + b << endl; // Calculates all operations
cout << "Product = " << a * b << endl;
cout << "Difference = " << a - b << endl;
cout << "Quotient = " << a / b << endl;
cout << "Remainder is " << a % b << endl;
cout << "Enter two decimal numbers" << endl;
cin >> c >> d; // Input two decimal numbers
cout << "Sum = " << c + d << endl; // Calculates all operations
cout << "Product = " << c * d << endl;
cout << "Difference = " << c - d << endl;
cout << "Quotient = " << c / d << endl;
cout << "Remainder is " << static_cast<int> (c) % static_cast<int> (d) << endl;
// Used static cast to change c and d to int and calculate remainder
system("pause");
return 0;
}
Explanation / Answer
using namespace std;
int main()
{
int a,b; // Declare Variables
int sum,difference,
float product;
cout << "Input two numbers" << endl;
cin >> a >> b; // Input two integers
sum = a + b; // Calculates all operations
out<<sum;
product= a * b;
out<<product;
difference =a - b ;
out<<difference;
if(b==0)
{out<<” division by 0 is not possible “;
}
else{
out << "Quotient = " << a / b << endl;
out << "Remainder is " << a % b << endl;
}
if(a>b)
{out<<” a is greater than b”;}
else
{out<<”b is greater than a”;
}
If(a==b)
{
out<<”a is equal to b”;
}
if((a%2)==0)
out<<”a is even”;
else
out<<” a is odd
if(b%2==0)
out<<”b is even”;
else
out<<”b is odd”;
out.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.